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

本文关键字:GridView 选择 任何行 是否 何检查 检查 | 更新日期: 2023-09-27 18:00:43

我在aspx页面中有一个网格视图:

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

我有一个确定按钮,当它被点击时,我会从页面收集数据。我想在单击"确定"按钮时检查是否有从网格视图中选择的行。

我怎样才能做到这一点?

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

您可以检查如下。。。

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

您可以尝试以下操作:

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

更好的方法:

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

如果选择的值是CCD_ 3,则对CCD_。

您也可以像这样检查

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