1

I have a GridView in a ASP.NET web application, in which I have added image button in each row:

 <asp:TemplateField>
       <ItemTemplate>
      <asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg" 
       ToolTip="Edit User Details"> </asp:ImageButton>
      </ItemTemplate> 
 </asp:TemplateField>

Now how I can get the row data from gridview simply by clicking an edit image button in a row?

2 Answers 2

2

You have to change the CommandArgument with this one:

CommandArgument="<%# ((GridViewRow) Container).RowIndex %>  

Then:

 protected void GridView1_RowCommand(object sender, 
  GridViewCommandEventArgs e)
{
  if (e.CommandName == "edituser") /*if you need this
  {
    // Retrieve the row index stored in the 
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the buttonfrom the Rows collection.
    GridViewRow row = GridView1.Rows[index];

    // Add code here now you have the specific row data
  }

  }
Sign up to request clarification or add additional context in comments.

Comments

0

Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");

        this.TextBox3.Text = _lblText.Text ;

    }

1 Comment

Getting error after using your code -Error Message:"Object reference not set to an instance of an object"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.