32

I have an asp.net GridView:

<asp:TemplateField HeaderText="View Faktor" ShowHeader="False" Visible="True">
    <ItemTemplate>
        <asp:ImageButton ID="imgBtn1" CssClass="SelectRow" runat="server" CausesValidation="false"
            CommandArgument='<%#(eval("mprID")) %>' CommandName="ViewFactors" ImageUrl="~/tadarokat/Images/factor.png"
            Text="" />
    </ItemTemplate>
</asp:TemplateField>

How Can I get rowIndex on row command event?

I want to highlight (select) target row when RowCommand fires.

5377037
  • 9,493
  • 12
  • 43
  • 73
Shahin
  • 11,703
  • 37
  • 120
  • 199

6 Answers6

70

this is answer for your question.

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

int RowIndex = gvr.RowIndex; 
clamchoda
  • 3,111
  • 2
  • 30
  • 63
rahularyansharma
  • 10,835
  • 17
  • 74
  • 127
13

ImageButton \ Button etc.

CommandArgument='<%# Container.DataItemIndex%>' 

code-behind

protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = e.CommandArgument;
}
Vincenzo Costa
  • 894
  • 11
  • 15
4

If you have a built-in command of GridView like insert, update or delete, on row command you can use the following code to get the index:

int index = Convert.ToInt32(e.CommandArgument);

In a custom command, you can set the command argument to yourRow.RowIndex.ToString() and then get it back in the RowCommand event handler. Unless, of course, you need the command argument for another purpose.

5377037
  • 9,493
  • 12
  • 43
  • 73
pallavi
  • 41
  • 1
3

Or, you can use a control class instead of their types:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

int RowIndex = row.RowIndex; 
5377037
  • 9,493
  • 12
  • 43
  • 73
1
protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Delete")
        {
            GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            int RemoveAt = gvr.RowIndex;
            DataTable dt = new DataTable();
            dt = (DataTable)ViewState["Products"];
            dt.Rows.RemoveAt(RemoveAt);
            dt.AcceptChanges();
            ViewState["Products"] = dt;
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}
protected void gvProductsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        gvProductsList.DataSource = ViewState["Products"];
        gvProductsList.DataBind();
    }
    catch (Exception ex)
    {

    }
}
5377037
  • 9,493
  • 12
  • 43
  • 73
Siddhesh
  • 11
  • 1
1

I was able to use @rahularyansharma's answer above in my own project, with one minor modification. I needed to get the value of particular cells on the row on which the user clicks a LinkButton. The second line can be modified to get the value of as many cells as you wish.

Here is my solution:

GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string typecore = gvr.Cells[3].Text.ToString().Trim();
5377037
  • 9,493
  • 12
  • 43
  • 73
Davezilla
  • 41
  • 5