如何检查是否从GridView中选择了任何行?

时间:2011-06-29 10:03:56

标签: c# asp.net gridview rowdatabound

我在aspx页面中有一个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按钮,当它被点击时,我从页面收集数据。我想查看确定按钮单击是否存在从Gridview中选择的任何行。

我怎样才能做到这一点? 任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:12)

您可以查看...

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

答案 1 :(得分:2)

您可以尝试这样的事情:

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

答案 2 :(得分:1)

更好的这个:

if(GridView1.SelectedIndex < 0)
    { its -1 and no row is selected.}
else
    {its >= 0 and a row is selected}
如果所选值为!= null,则

null进行测试会抛出异常。

答案 3 :(得分:0)

您也可以这样检查

if(GridView.SelectedIndex >= 0)
   {
      string result = "Selected";
   }
    else
   {
     string result = "Not Selected";
   }