How to get the selected row index in OnRowCommand of GridView

Posted by Webmaster under ASP.NET on 9/6/2008 | Views : 14490 | Status : [Administrator] | Replies : 2
I have a GridView and trying to get the selected Row Index of the GridView in OnRowCommand but there is no property called RowIndex in the eventargs.

_________________________
Posted on behalf of Raja

Best regards,
Webmaster
http://www.dotnetfunda.com



Responses

Posted by: Webmaster on: 9/6/2008 [Administrator] HonoraryPlatinum

Up
0
Down
Specify CommandArgument value as Container.DataItemIndex like below

<asp:LinkButton ID="lnkDel" runat="server" Text="X" CommandName="remove" CommandArgument='<%# Container.DataItemIndex %>' />




Access the row index as int rowIndex = int.Parse(e.CommandArgument.ToString()); get the selected row as GridViewRow row = GridView1.Rows[index];

________________________
Posted on behalf of Poster


Best regards,
Webmaster
http://www.dotnetfunda.com

Webmaster, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Poster on: 10/14/2008 [Member] Starter

Up
0
Down
Update: This will not work if your Gridview will paginating. So use <%# DataBinder.Eval(Container, "RowIndex") %> instead of <%# Container.DataItemIndex %> to work in all conditions.

Webmaster, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response