Welcome to AspAdvice Sign in | Join | Help

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='<%# Eval("id") %>' 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;
 

Published Friday, May 15, 2009 1:48 PM by mo meng

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Monday, July 20, 2009 6:22 PM by HelloWorld

# re: ASP.NET - GridView RowCommand get row index

At #2 , what happens if the ids of the entries dont much the row numbers after an entry is deleted from database?
Sunday, August 09, 2009 1:52 PM by Azat

# re: ASP.NET - GridView RowCommand get row index

Thank you very much! That helped me!! ))
Wednesday, September 23, 2009 7:04 AM by Sheeba

# re: ASP.NET - GridView RowCommand get row index

Really good and quite useful
Monday, September 28, 2009 4:26 AM by mo meng

# re: ASP.NET - GridView RowCommand get row index

dont really get what you mean but i will try to answer this,

rows number is basically just the row in the grid, the important is the id assigned as the key to the row itself

if user A and user B at the same time viewing the same result on different PC e.g. and user A delete one row, when user B attempt to delete the same row, no physical data deletion in the table in database will need to take place, just a rebind method will followup to refresh the grid

Wednesday, October 21, 2009 11:54 AM by ASP.Net Requester

# re: ASP.NET - GridView RowCommand get row index

Excellent !!! Thank you very much for posting the solution. The following worked perfect for me. If you want to highlight a selected row in the GridView, the following approach worked.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

GridViewRow selectedRow = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;

int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);

GridView.Rows[intRowIndex].BackColor = System.Drawing.Color.Blue;

}

Wednesday, November 11, 2009 1:50 PM by Gus

# re: ASP.NET - GridView RowCommand get row index

Thanks
Monday, November 30, 2009 5:56 AM by jvschalk

# re: ASP.NET - GridView RowCommand get row index

Thank you SOOOO MUCCCHHHH! Been searching the whole morning and it turned out to be so simple. The best solution to my problem i've seen today. Thanks again
Saturday, December 12, 2009 7:37 AM by Ratan Mishra

# re: ASP.NET - GridView RowCommand get row index

Thank You very much it worked well for me. My Code : Dim resourceID As Integer Dim selectedRow As GridViewRow = GV1.Rows(Convert.ToInt32(e.CommandArgument)) Dim rowIndex As Integer = selectedRow.RowIndex resourceID = Convert.ToInt16(GV1.Rows(rowIndex).Cells(1).Text.ToString()) Server.Transfer(("Registration.aspx?resourceID=" & resourceID))
Tuesday, January 12, 2010 9:40 PM by A

# re: ASP.NET - GridView RowCommand get row index

Thanks
Monday, January 25, 2010 4:38 AM by Yagnesh

# re: ASP.NET - GridView RowCommand get row index

thanks its working

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below