Shantanu Sen
Shantanu Sen

Reputation: 221

Get Gridview Row Index

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

'> '>

Upvotes: 2

Views: 12787

Answers (2)

GeorgesD
GeorgesD

Reputation: 1082

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();
    }

  }

Upvotes: 1

Pseudonym
Pseudonym

Reputation: 2072

GridViewRow row = gvESAPending.Rows[index];

By doing this your actually accessing the row at index. So if index = 2 your actually returning the third row in your gridviewrow.

Upvotes: 0

Related Questions