问题
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? Any help would be appreciated.
回答1:
You can check like...
if (GridView1.SelectedValue != null)
{
//Row is Selected
}
回答2:
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
回答3:
Better this:
if(GridView1.SelectedIndex < 0)
{ its -1 and no row is selected.}
else
{its >= 0 and a row is selected}
testing for != null
will throw an exception if the selected value is null
.
回答4:
you can also check like this
if(GridView.SelectedIndex >= 0)
{
string result = "Selected";
}
else
{
string result = "Not Selected";
}
来源:https://stackoverflow.com/questions/6518886/how-to-check-if-any-row-is-selected-from-gridview