How to create and Bind datatable in Grid View


In this article i am going to build a datatable and bind it into gridview using asp.net c# very simple way. datatable class in C# ADO.Net store the data in grid form. datatable class in provied by System.data; nameapace. 

Step 1.  Open visual Studio and create a empty website . 





Step 2. Add a web form in the website. 






Step 3. Add gridview control inside form tag on web form page designer side by add this line given below. 

 
  <asp:GridView ID="gvbind" runat="server"></asp:GridView>
  


Step 4. Add namespace on  code behind file

       
   using System.Data;


Step 5. Add data table bind code on code behind file under page_load method.

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");


        dt.Rows.Add("abc","22");
        dt.Rows.Add("xyz", "23");
        dt.Rows.Add("pqr", "24");

        

        gvbind.DataSource = dt;
        gvbind.DataBind();


    }


Result :-  


Run visual studio application