I have a gridview on my aspx page set up the OnRowCommand event using a series of ASP.NET LinkButton object to handle the logic using the CommandName property. I need to access the GridViewRow.RowIndex to retrieve values from the selected row and notice it is a non-public members of the GridViewCommandEventArgs object while debugging the application
Is there a way I can access this property of is theere a better implementation?
Here is my source code:
aspx page:
<asp:GridView ID="MyGridView" runat="server" OnRowCommand="MyGirdView_OnRowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton
id="MyLinkButton"
runat="server"
CommandName="MyCommand"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
protected void MyGirdView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
//need to access row index here....
}
UPDATE:
@brendan - I got the following compilation error on the following line of code:
"Cannot convert type 'System.Web.UI.WebControls.GridViewCommandEventArgs' to 'System.Web.UI.WebControls.LinkButton'"
LinkButton lb = (LinkButton) ((GridViewCommandEventArgs)e.CommandSource);
I slightly modified the code and the following solution worked:
LinkButton lb = e.CommandSource as LinkButton;
GridViewRow gvr = lb.Parent.Parent as GridViewRow;
int gvr = gvr.RowIndex;