Ali.Rashidi
Ali.Rashidi

Reputation: 1462

GridView SelectedValue is null after selecting a row

I have a simple GridView with a Select button. in the GridView.RowCommand event when I select a row I want to read the GridView.SelectedValue it is null. The only time GridView.SelectedValue is valued with the right data is when I select the row twice.

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        code_bimehTextBox.Text = GridView1.SelectedValue.ToString();//SelectedValue is null after clicking the select button but after clicking again it works right
    }

Upvotes: 0

Views: 1125

Answers (2)

Cuong Ngo
Cuong Ngo

Reputation: 76

// get the values of selected row from a Gridview and display the values in textboxes using C# code.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
      TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;

  }

Upvotes: 1

Nemanja Todorovic
Nemanja Todorovic

Reputation: 2800

You are using wrong event. When using select button you should use SelectedIndexChanging and SelectedIndexChanged events. Here is where you can find complete list of buttons/events: GridView.RowCommand Event

Upvotes: 1

Related Questions