Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating Data Table and Columns on Runtime and Link with Data Grid in .NET 2.0.

Author: DevASP
Download Source Code : 612_DataTable_RunTime.zip

In this simple article you will learn that how you can Create Data Table and Columns on Runtime and Link with Data Grid.

Simply just create New Project of console application in C#.

 

Drag Six text box and a DataGrid on form.

 

 

Now just write code against these events

 

 

private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.Columns.Add(txt1.Text );

            dt.Columns.Add(txt2.Text );

            dt.Columns.Add(txt3.Text);

            dt.Columns.Add(txt4.Text);

            dt.Columns.Add(txt5.Text);

            dt.Columns.Add(txt6.Text);

           

           

            ds.Tables.Add(dt);

            dataGrid1.DataSource = ds;

            dataGrid1.Expand(-1);

            dataGrid1.DataMember = ds.Tables[0].ToString();

            dataGrid1.Expand(-1);

            error.Text = "";

        }

        catch (Exception Ex)

        {

            error.Text = "Two Cloumns Name Should Not Be Same";

        }

        }

 

        private void txt1_TextChanged(object sender, EventArgs e)

        {

         try

         {   DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.Columns.Add(txt1.Text);

            dt.Columns.Add(txt2.Text);

            dt.Columns.Add(txt3.Text);

            dt.Columns.Add(txt4.Text);

            dt.Columns.Add(txt5.Text);

            dt.Columns.Add(txt6.Text);

 

 

            ds.Tables.Add(dt);

            dataGrid1.DataSource = ds;

            dataGrid1.Expand(-1);

            dataGrid1.DataMember = ds.Tables[0].ToString();

            dataGrid1.Expand(-1);

            error.Text = "";

        }

        catch (Exception Ex)

        {

            error.Text = "Two Cloumns Name Should Not Be Same";

        }

        }

 

        private void txt2_TextChanged(object sender, EventArgs e)

        {

            try

            {

                DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.Columns.Add(txt1.Text);

            dt.Columns.Add(txt2.Text);

            dt.Columns.Add(txt3.Text);

            dt.Columns.Add(txt4.Text);

            dt.Columns.Add(txt5.Text);

            dt.Columns.Add(txt6.Text);

 

 

            ds.Tables.Add(dt);

            dataGrid1.DataSource = ds;

            dataGrid1.Expand(-1);

            dataGrid1.DataMember = ds.Tables[0].ToString();

            dataGrid1.Expand(-1);

            error.Text = "";

        }

        catch (Exception Ex)

        {

            error.Text = "Two Cloumns Name Should Not Be Same";

        }

        }

 

        private void txt3_TextChanged(object sender, EventArgs e)

        {

            try{

                DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.Columns.Add(txt1.Text);

            dt.Columns.Add(txt2.Text);

            dt.Columns.Add(txt3.Text);

            dt.Columns.Add(txt4.Text);

            dt.Columns.Add(txt5.Text);

            dt.Columns.Add(txt6.Text);

 

 

            ds.Tables.Add(dt);

            dataGrid1.DataSource = ds;

            dataGrid1.Expand(-1);

            dataGrid1.DataMember = ds.Tables[0].ToString();

            dataGrid1.Expand(-1);

            error.Text = "";

             }

            catch (Exception Ex)

            {

                error.Text = "Two Cloumns Name Should Not Be Same";

            }

        }

 

        private void txt4_TextChanged(object sender, EventArgs e)

        {

            try

            {

            DataSet ds = new DataSet();

            DataTable dt = new DataTable();

           

                dt.Columns.Add(txt1.Text);

                dt.Columns.Add(txt2.Text);

                dt.Columns.Add(txt3.Text);

                dt.Columns.Add(txt4.Text);

                dt.Columns.Add(txt5.Text);

                dt.Columns.Add(txt6.Text);

                ds.Tables.Add(dt);

                dataGrid1.DataSource = ds;

                dataGrid1.Expand(-1);

                dataGrid1.DataMember = ds.Tables[0].ToString();

                dataGrid1.Expand(-1);

                error.Text = "";

            }

            catch (Exception Ex)

            {

                error.Text = "Two Cloumns Name Should Not Be Same";

            }

        }

 

        private void txt5_TextChanged(object sender, EventArgs e)

        {

            try{

                DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.Columns.Add(txt1.Text);

            dt.Columns.Add(txt2.Text);

            dt.Columns.Add(txt3.Text);

            dt.Columns.Add(txt4.Text);

            dt.Columns.Add(txt5.Text);

            dt.Columns.Add(txt6.Text);

 

 

            ds.Tables.Add(dt);

            dataGrid1.DataSource = ds;

            dataGrid1.Expand(-1);

            dataGrid1.DataMember = ds.Tables[0].ToString();

            dataGrid1.Expand(-1);

            error.Text = "";

        }

        catch (Exception Ex)

        {

            error.Text = "Two Cloumns Name Should Not Be Same";

        }

        }

 

        private void txt6_TextChanged(object sender, EventArgs e)

        {

            try{

                DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.Columns.Add(txt1.Text);

            dt.Columns.Add(txt2.Text);

            dt.Columns.Add(txt3.Text);

            dt.Columns.Add(txt4.Text);

            dt.Columns.Add(txt5.Text);

            dt.Columns.Add(txt6.Text);

 

 

            ds.Tables.Add(dt);

            dataGrid1.DataSource = ds;

            dataGrid1.Expand(-1);

            dataGrid1.DataMember = ds.Tables[0].ToString();

            dataGrid1.Expand(-1);

            error.Text = "";

        }

        catch (Exception Ex)

        {

            error.Text = "Two Cloumns Name Should Not Be Same";

        }

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            this.BackColor = Color.Ivory  ;

            this.Text = "DevAsp";

 

            label1.Text = "Column1";

            label2.Text = "Column2";

            label3.Text = "Column3";

            label6.Text = "Column4";

            label5.Text = "Column5";

            label4.Text = "Column6";

 

            txt1.Text = "Column1";

            txt2.Text = "Column2";

            txt3.Text = "Column3";

            txt4.Text = "Column4";

            txt5.Text = "Column5";

            txt6.Text = "Column6";

 

        }

    }

 

 

Article Comments
this code helped me a lot. thanks

please send me more logical codes releated to data grid

Posted on 2/3/2007 2:15:35 AM by kittu

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Copy and Delete a Specific file with C#.net application in .NET 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net