Adsence750x90

Wednesday, April 28, 2010

Dynamic DataTable ASP.Net

This Article for How to Create a DataTable Dynamically.

DataTable is Derived from System.Data namespace


//Create a object of Datatable
        DataTable dynamicTable = new DataTable();

        //DataRow
        DataRow dynamicRow;
        //DataColumn for Name and Time
        DataColumn NameColumn = new DataColumn("Name", typeof(string));
        DataColumn TimeColumn = new DataColumn("Time", typeof(string));

        //add Column to Datatable
        dynamicTable.Columns.Add(NameColumn);
        dynamicTable.Columns.Add(TimeColumn);
        //loop for 10 item in DataTable
        for (int i = 0; i < 10; i++)
        {
            dynamicRow = dynamicTable.NewRow();
            //Add Rows to dymanicTable
            dynamicTable.Rows.Add(dynamicRow);
            //Assign Column value
            dynamicTable.Rows[i][NameColumn] = Guid.NewGuid().ToString();
            dynamicTable.Rows[i][TimeColumn] = DateTime.Now.ToString();
        }

        //Bind DynamicTable into a GridView
        GridView1.DataSource = dynamicTable;
        GridView1.DataBind();

4 comments:

Anonymous said...

Excellent Dear this help me a lot thnx

Bhaskar said...

thanks for sharing But i want to know how to perform the operation on data table like edit,delete etc

Bhaskar said...

Thanks for sharing this but i want know how to perform operations like edit,delete on data table

Anonymous said...

Thanks buddy