Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the indexes of selected rows in GridView

I want get the rows I selected from gridview use a checkbox. The checkbox is like this! <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> And I want to get one column in each row. How to do it.thx!

like image 539
Justin Avatar asked Apr 08 '11 10:04

Justin


2 Answers

try this:

protected void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)checkbox.NamingContainer;
    if (checkbox.Checked == true) {
        row.BackColor = System.Drawing.Color.Red;
        mygridview.Columns(0).Visible = false;
    }
}
like image 99
Pranay Rana Avatar answered Sep 29 '22 13:09

Pranay Rana


You can loop through the GridView rows and use FindControl to retrieve the Checkbox and then get the IsChecked property on them.

foreach (GridViewRow row in grid.Rows)
{
  CheckBox check = (CheckBox)row.FindControl("CheckboxID");

  if (CheckBox1.Checked)
  {
   ...
  } 
}
like image 34
Alessandro Avatar answered Sep 29 '22 12:09

Alessandro