获取GridView中选定行的索引

本文关键字:索引 GridView 获取 | 更新日期: 2023-09-27 17:49:18

我想获得我从gridview使用复选框选择的行。复选框是这样的!<asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" /> </ItemTemplate> </asp:TemplateField>我想在每一行中得到一列。怎么做呢?谢谢!

获取GridView中选定行的索引

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;
    }
}

你可以循环遍历GridView行,并使用FindControl来检索复选框,然后获得它们的IsChecked属性。

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