C# – Displaying the DbSet Result in an ASP.Net Gridview

Gridview server control – Databinds to enumerable collections of objects and renders in a tabular format.

Must call ToList() on DbSet to bind to a databound control.

gridControl.DataSource = dbCustomers.ToList();
gridControl.DataBind();

Implementing a Button Command in a GriedView
Click Chevron -> GridView Taks -> Edit Columns…

BoundField – Databind to a object property
ButtonField – Hyperlink Button

Handle button click in the GridView_RowCommand event handler.

protected void GriedView1_RowCommand(object sender, GridViewCommandEventArgs e){
  //Retrieve the ROW CLICKED in the grid
  int index = Convert.ToInt32(e.CommandArgument);
  GridView1 row = GridView1.Rows[index]

  //Accessing the cells is risky because the order of the columns may change
  //over time and you might forget that this depends on it. Also ..... 0 based!
  var someValue = row.Cells[1].Text;
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s