Get Gridview Row Index

10,693

You can add OnRowCreteEvent

ASPX:

<asp:gridview id="gvESAPending" onrowcreated="gvESAPending_RowCreated" ...

CS :

protected void gvESAPending_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }
Share:
10,693
Shantanu Sen
Author by

Shantanu Sen

Updated on June 13, 2022

Comments

  • Shantanu Sen
    Shantanu Sen almost 2 years

    I am using the below code to get the row index

    protected void gvESAPending_RowCommand(object sender, GridViewCommandEventArgs e)
    
        {
            try
            {
                lblMsg.Text = "";
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvESAPending.Rows[index]; // Here incorrect format error is coming
            }
         }
    

    But the index value is coming as 0. What is wrong here?

    Aspx Code

    '> '>

  • Shantanu Sen
    Shantanu Sen over 11 years
    I got this error {System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'.
  • GeorgesD
    GeorgesD over 11 years
    What is the error ? Can you give us the aspx code or follow the recommendation of @huMpty duMpty
  • Shantanu Sen
    Shantanu Sen over 11 years
    int index = Convert.ToInt32( e.CommandArgument); This is returning 0 so it should work. But its giving error
  • Shantanu Sen
    Shantanu Sen over 11 years
    <asp:ButtonField ButtonType="Button" CommandName="App" Text="Approve"> <ControlStyle BackColor="White" BorderStyle="None" Font-Underline="True" ForeColor="Blue" /> </asp:ButtonField> <asp:ButtonField ButtonType="Button" CommandName="Dec" Text="Decline"> <ControlStyle BackColor="White" BorderStyle="None" Font-Underline="True" ForeColor="Blue" /> </asp:ButtonField>
  • Shantanu Sen
    Shantanu Sen over 11 years
    How will on rowcreate help .. I am not creating any row
  • GeorgesD
    GeorgesD over 11 years
    Ok that why the index value is coming as 0. Can you explain what you wanna do ??
  • Shantanu Sen
    Shantanu Sen over 11 years
    Already the gridview is bound with data .. I am clicking a buttonfield and thus need to get the row number
  • GeorgesD
    GeorgesD over 11 years
    gridview is bound with data so rows are created !
  • Shantanu Sen
    Shantanu Sen over 11 years
    all the examples are showing can be achieved by gvESAPending_RowCommand, why my code is not working .. am i missing something4
  • Shantanu Sen
    Shantanu Sen over 11 years
    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
  • Shantanu Sen
    Shantanu Sen over 11 years
    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
  • Pseudonym
    Pseudonym over 11 years
    try this Int32.Parse(e.CommandArgument.ToString()) something may be screwy with the convert function, this should boil it down and give you something more useful to work with. If you really want to get fancy set string var = e.CommandArgument.ToString() put a break point right after it and check it out in the locals window to make sure your getting a value