user366312
user366312

Reputation: 16894

ASP.NET - GridView

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

Upvotes: 0

Views: 371

Answers (3)

JBrooks
JBrooks

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

Canavar
Canavar

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

user434917
user434917

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

Related Questions