Donate

Retrieve Current Row Index Of ASP.NET Web Forms GridView Control On RowCommand

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.
ASPX Code
<asp:TemplateField>
 <ItemTemplate>
  <asp:LinkButton ID="lbtnDelete" CSSClass="btn" CommandName="Delete" Text="Delete" runat="server"/>
 </ItemTemplate>
</asp:TemplateField>
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...
 }
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.