2011-06-29 16 views
5

Tengo un gridview en la página aspx:¿Cómo comprobar si se selecciona alguna fila de 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> 

El Método rowDataBound es:

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

que tienen un botón Aceptar, cuando se hace clic, colecciono datos de la página. Quiero comprobar en el botón Aceptar clic que hay alguna fila seleccionada de Gridview o no.

¿Cómo puedo lograrlo? Cualquier ayuda sería apreciada.

Respuesta

10

Puede comprobar como ...

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

Funcionó muy bien !! – asma

1

Usted puede intentar algo como esto:

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

Mejor esto:

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

pruebas para != null será una excepción si el valor seleccionado es null.

Cuestiones relacionadas