2013-05-09 58 views
1

我想檢查是否選擇了gridview中的一行。我如何寫條件?C#檢查gridview行選擇與否

if (gridview-row[1].select) 

{ 
    //do something 
} 

else 

{ 
    //do something 
} 
+0

什麼樣的應用程序,ASP.Net? WinForm ?? WPF – Habib 2013-05-09 04:41:04

回答

0

你可以橫置GridViewRow.RowState財產Asp.net Gridview.RowState

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

,並檢查這樣

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

假設你的問題是asp.net,你可以設置一個SelectedIndexChanged事件上GridView。只要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; 
    } 
0
if (GridView.SelectedIndex == rowIndex) 
{ 
    // do something 
} 
else 
{ 
    // do something 
} 

你可以這樣寫。