Wednesday, August 31, 2016

Retrieve current row index of an ASP.NET GridView control on RowCommand event

Hi, To retrieve the current row index of a GridView control on RowCommand event, you have to access the NamingContainer property of a control which belongs to that GridViewRow. In the example below, I have a LinkButton control declared inside a TemplateField.
  <asp:LinkButton ID="lbtnDelete" CSSClass="btn" CommandName="Delete" Text="Delete" runat="server"/>
Code Behind
protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
 if (e.CommandName == "Delete")
  GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
  int RowIndex = gvr.RowIndex;
  //TODO: other codes here...


Post a Comment