How to check if any row is selected from GridView?

asma picture asma · Jun 29, 2011 · Viewed 23.7k times · Source

I have a gridview in aspx page:

<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>

The rowDataBound Method is:

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

I have an OK button, when it is clicked, I collect data from page. I want to check on OK button click that is there any row selected from Gridview or not.

How can I achieve this?

Answer

Muhammad Akhtar picture Muhammad Akhtar · Jun 29, 2011

You can check like...

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