Skip to content

Checkboxes inside a GridView

If you want to use checkboxes in GridViews you have several options.

  1. The CheckBoxField control is a one-way bound control which needs a respective Boolean data field in the data source.
  2. The regular asp:Checkbox inside an itemTemplate (or editTemplate).

A common scenario for option number 2 is a CheckBox for each item inside the GridView that lets you chose whether that item should be deleted. In this case you would iterate through every GridView row to determine if it was checked or not. Here is an implementation for this example.

You can also implement the CheckChanged event. In this case you would have to address the GridView row since the checkboxes are not distinct by their unique ID.

protected void ckbView_CheckChanged(object sender, EventArgs e)
{
    CheckBox ckb = sender as CheckBox;

    if (ckb != null)
    {
        GridViewRow r = ckb.NamingContainer as
         GridViewRow;

	… do stuff ...
    }
}

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*