C#检查gridview行是否选择

时间:2013-05-09 04:37:30

标签: c# .net gridview

我想检查是否选择了gridview中的行。我怎么能写条件??

if (gridview-row[1].select)

{
    //do something
}

else

{
   //do something
}

3 个答案:

答案 0 :(得分:0)

您可以使用GridViewRow.RowState属性Asp.net Gridview.RowState

GridViewRow row = yourGridView.SelectedRow; will give the selected row 

并像这样检查

if(row.RowState == DataControlRowState.Selected)
    {
      //selectd
    } 

答案 1 :(得分:0)

假设您的问题是asp.net,您可以在GridView上设置SelectedIndexChanged事件。只要GridView有新的行选择,就会触发此事件。更多信息here

  <asp:gridview id="CustomersGridView" 
       datasourceid="CustomersSource" 
       autogeneratecolumns="False"
       autogenerateselectbutton="True"
       allowpaging="True" 
       selectedindex="1"
       onselectedindexchanged="CustomersGridView_SelectedIndexChanged" 
       runat="server" DataKeyNames="CustomerID"> ...

您可以在其中获取所选行的事件侦听器

  void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
  {
    // Gets the currently selected row based on the selected index changed event
    //
    var selectedRow = CustomersGridView.SelectedRow;
  }

答案 2 :(得分:0)

if (GridView.SelectedIndex == rowIndex)
{
    // do something
}
else
{
    // do something
}

你可以这样写。