Follow by Email

Thursday, June 20, 2013

asp.net(C#) adding columns and rows for datatable in Codebehind | Example for adding rows, column manually in datatable from codebehind

hi in this post i will show how to manually add columns and rows in datatable from a codebehind page in asp.net (c#).

Below is the sample code for this :

public void createDatatable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Salary");

        DataRow firstRow = dt.NewRow();
        firstRow["Name"] = "Chandan";
        firstRow["Salary"] = "1000";

        dt.Rows.Add(firstRow);

        DataRow SecondRow = dt.NewRow();
        SecondRow["Name"] = "Subodh";
        SecondRow["Salary"] = "2000";

        dt.Rows.Add(SecondRow);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }