Monday, May 10, 2010

ASP.NET - GridView RowCommand get row index

if you did assign a value into the CommandArgument e.g. below, you need to use the #1 methond else #2
<itemtemplate> <asp:imagebutton id="imgUpdate" runat="server" commandname="Update1" commandargument="'<%#">' ImageUrl="~/icons/Update001 (2).gif" /></itemtemplate>
#1
VB
Dim selectedRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)Dim intRowIndex As Integer = Convert.ToInt32(selectedRow.RowIndex)GridView.Rows(intRowIndex).BackColor = System.Drawing.Color.Blue
C#
GridViewRow selectedRow = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);GridView.Rows[intRowIndex].BackColor = System.Drawing.Color.Blue;
or #2
VB
Dim selectedRow As GridViewRow = GridView.Rows(Convert.ToInt32(e.CommandArgument))selectedRow.BackColor = System.Drawing.Color.Blue
C#
GridViewRow selectedRow = GridView.Rows[Convert.ToInt32(e.CommandArgument)];selectedRow.BackColor = System.Drawing.Color.Blue;

No comments: