views:

92

answers:

3

How to work with ASP.net GridView programmatically (i.e. without Data binding)?

A: 

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();
Marwan Aouida
-1 He mentioned NOT using data binding
Cody C
I think he means by databinding "binding to the database" not the Databind method.
Marwan Aouida
A: 

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.

Canavar
+1  A: 

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();
        }
    }
JBrooks