Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if any row is selected from GridView?

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?

like image 853
asma Avatar asked Jun 29 '11 10:06

asma


2 Answers

You can check like...

if (GridView1.SelectedValue != null)
{
     //Row is Selected
}
like image 59
Muhammad Akhtar Avatar answered Oct 05 '22 11:10

Muhammad Akhtar


You can try something like this:

If GridView1.SelectedRows.Count > 0 Then
' yourcode here - a row is selected 
Else
' yourcode here - NO row is selected 
End If
like image 30
Isaac Njiru Avatar answered Oct 05 '22 11:10

Isaac Njiru