Reputation: 16894
How to work with ASP.net GridView programmatically (i.e. without Data binding)?
Upvotes: 0
Views: 371
Reputation: 10013
I like to use the DataTable, so I would do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Include", typeof(Boolean));
dt.Columns.Add("Name", typeof(String));
dt.Rows.Add(new Object[] { 0, "Jim" });
dt.Rows.Add(new Object[] { 0, "Jen" });
dt.Rows.Add(new Object[] { 0, "Kylie" });
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Upvotes: 1
Reputation: 48088
There is no way to access the rows of the gridview like myGrid.Rows.Add().
You can update datasource before binding it to your gridview.
Upvotes: 0
Reputation:
just set the DataSource property to the object you want to dispaly then call the DataBind method.
var items = new List<string> {"item1","item2","item3"};
GridView1.DataSource = items;
GridView1.DataBind();
Upvotes: 0