2011-06-29 23 views
5

私はaspxページ内のGridViewを持っている:GridViewから行が選択されているかどうかを確認する方法は?

<asp:GridView ID="gdvMainList" runat="server" CssClass="Grid1" SkinID="PagedGridView" 
            AutoGenerateColumns="false" OnRowDataBound="gdvMainList_RowDataBound" 
            DataSourceId="dtsConsumers" Visible="false" DataKeyNames="Id"> 
            <Columns> 
             <asp:CommandField SelectText="Select" ShowSelectButton="true" ItemStyle-CssClass="HideButton" 
              HeaderStyle-CssClass="HideButton"> 
              <HeaderStyle CssClass="HideButton" /> 
              <ItemStyle CssClass="HideButton" /> 
             </asp:CommandField> 
             <asp:TemplateField HeaderText="Name"> 
              <ItemTemplate> 
               <span> 
                <%# Pc.PrecisionCare2.PL.Common.Utility.GetFullName("", Eval("LastName"), Eval("FirstName"), Eval("MiddleInit")) %></span> 
              </ItemTemplate> 
              <ItemStyle Width="200px" /> 
             </asp:TemplateField> 
             <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status"></asp:BoundField> 
            </Columns> 
            <SelectedRowStyle CssClass="SelectedItem" BackColor="#c9e0ee" /> 
            <EmptyDataTemplate> 
             <div class="divEmptyGrid"> 
              --- No Consumer Exists --- 
             </div> 
            </EmptyDataTemplate> 
           </asp:GridView> 

rowDataBound方法は次のとおりです。

protected void gdvMainList_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gdvMainList, "Select$" + e.Row.RowIndex); 
      } 
     } 

それがクリックされたとき、私は、OKボタンを持って、私はページからデータを収集します。 OKボタンをクリックしてチェックしたいのですが、Gridviewから選択された行があります。

どうすればこの問題を解決できますか? ご協力いただければ幸いです。

答えて

10

あなたが好き確認することができます...

if (GridView1.SelectedValue != null) 
{ 
    //Row is Selected 
} 
+0

素晴らしいです! – asma

1

あなたはこのような何か試すことができます。より良い

If GridView1.SelectedRows.Count > 0 Then 
' yourcode here - a row is selected 
Else 
' yourcode here - NO row is selected 
End If 
1

をこの:!= nullため

if(GridView1.SelectedIndex < 0) 
    { its -1 and no row is selected.} 
else 
    {its >= 0 and a row is selected} 

テストがあれば例外をスローします選択された値はnullです。

関連する問題