Tahir Hassan's Blog

My Technical Notes

Friday, 19 July 2013

Iterating over the Rows in a GridView

When using the GridView, it is sometimes necessary to access the same control from each row in a GridView, for example, a CheckBox which the user can either tick or leave unticked.


foreach (GridViewRow gridViewRow in GridView1.Rows)
{
var checkBox = gridViewRow.FindControl("ZeroCheckBox") as CheckBox;
if (checkBox.Checked)
{
// get the id of the row
int dbRowID = (int)GridView1.DataKeys[gridViewRow.RowIndex].Value;
}
}