-2

I needed to dynamically generate data for a GridView, so I built a DataTable to store the data. When I tried to bind the DataTable to GridView, there's nothing displayed on the GridView. I tried debugging and verified that data are being stored into the DataTable correctly.

There is no code on the aspx side, just a gridview.

  // C# code


DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("C1", typeof(string)));
dt.Columns.Add(new DataColumn("C2", typeof(string)));
dt.Columns.Add(new DataColumn("C3", typeof(string)));
DataRow dr = dt.NewRow();
dr["C1"] = "11";
dr["C2"] = "22";
dr["C3"] = "33";
dt.Rows.Add(dr);
GridView2.DataSource = dt;
GridView2.DataBind();
  • check AutoGenerateColumns columns property of gridview..and set to true – Shubham Feb 14 '19 at 11:02
  • Do you create the columns in the GridView manually or have you set `AutoGenerateColumns="true"`? – Markus Feb 14 '19 at 11:02
  • @Shubham Your code looks fine from what I can see. Only logical explanation is what Markus has suggested. Check your gridview control. – nick gowdy Feb 14 '19 at 11:04
  • The code looks fine. Things to check:1)AutoGenerateColumns property set to true. 2)GridView2's Visible property set to true. – Gagan Deep Feb 14 '19 at 11:25

1 Answers1

0

May be you should try this below link to check if you have used the correct method to create a datatable. The link is as follows Creating a DataTable object with dummy data And also as Marcus told. You should check the AutoGenerateColumns property.

Thameem
  • 700
  • 1
  • 13
  • 38