Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the cell value of a GridView row

I am using the GridView - AutoGenerateSelectButton = "True" to select the row in order to get the Column 1 cell value.

I have tried:

GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";

And it writes the "Cell Value" but nothing else.

I finally was able to get the number of the row (index) but not the cell value.

GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = row.RowIndex.ToString();

I have tried:

TextBox1.Text = dgCustomer.Rows[row.RowIndex].Cells[1].Text;

and still returns the index row.

Any other suggestions? Thank you!

like image 920
Lily Avatar asked Aug 18 '14 17:08

Lily


1 Answers

Try changing your code to

// Get the currently selected row using the SelectedRow property.
GridViewRow row = dgCustomer.SelectedRow;

// And you respective cell's value
TextBox1.Text = row.Cells[1].Text

UPDATE: (based on my comment) If all what you are trying to get is the primary key value for the selected row then an alternate approach is to set

datakeynames="yourprimarykey"

for the gridview definition which can be accessed from the code behind like below.

TextBox1.Text = CustomersGridView.SelectedValue.ToString();
like image 57
Dennis R Avatar answered Sep 23 '22 04:09

Dennis R