Monday, November 16, 2009

GridView: get selected rows

Below is sample C# code to get selected rows in GridView.

protected int[] GetSelectedRows()
{
List rowIDs = new List();
foreach (GridViewRow row in GridView.Rows)
{
CheckBox cb = ((CheckBox)row.FindControl("chkRow"));
if (cb.Checked)
{
string IDString = GridView.DataKeys[row.RowIndex].Value.ToString();
rowIDs.Add(Int32.Parse(IDString));
}
}
return rowIDs.ToArray();
}

No comments: