0

How to get value of row in Gridview and display in Textbox?

Don't work this code. And I don't want to use this code:

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
        LinkButton lnkButton = sender as LinkButton;
        GridViewRow row = (GridViewRow) lnkButton.NamingContainer;
        lblSender.Text = row.Cells[2].Text;
        lblSubject.Text = row.Cells[5].Text;
        txtReadMsg.Text = row.Cells[6].Text;
8
  • What grid event are you doing that in? Commented Jan 25, 2013 at 15:36
  • protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { } Commented Jan 25, 2013 at 15:38
  • You will be getting lots of down votes asking the question that way. Elaborate please, nobody can help you if you don´t explain properly. What does not works? Does the row assignment works properly? Commented Jan 25, 2013 at 15:39
  • 1
    Show us the aspx markup. Commented Jan 25, 2013 at 15:41
  • 2
    What objects are in the cells of your GridView? You may want to use FindControl to do what you want, but I'm not sure. Something like TextBox myTextBox = (TextBox)myGridView.Rows[e.RowIndex].FindControl("TextBoxInGridView"); then lblSender.Text = myTextBox.Text; Commented Jan 25, 2013 at 15:48

2 Answers 2

0

Are you sure your GridView's cells don't have controls inside it.

maybe this helps you

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

Comments

0

To get the row, do:

var row = GridView1.Rows[e.NewSelectedIndex];

And then the rest of the code should work for you. You can then remove the first two lines. Also, note Text only works if you are using BoundColumn column definitions; it won't work if you are using a different type of column.

If you are getting a different error, then it's related to something else; please add a comment and we can help work through it.

Comments

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.