Tuesday, November 17, 2009

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
<asp:ItemTemplate>
<asp:asp:ImageButton ID="imgUpdate" runat="server" CommandName="Update1"
CommandArgument='<%# Eval("id") %>' ImageUrl="~/icons/Update001 (2).gif" />
<asp:/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:

Post a Comment