Getting data from a selected row in a gridview

N

Neil

I have a gridview which is being populated with no problems.

I want to be able to reference the data from the cells in the row but
having followed an example on MSDN cannot get any data to be displayed
in a text box no matter which cell index I provide.

I tried a row of code that successfully displays the rowindex of the
selected row.

The code with comments is below:

protected void GridView1_SelectedIndexChanged(object sender,
EventArgs e)
{
// Get the currently selected row using the SelectedRow
property.
GridViewRow row = GridView1.SelectedRow;

//This row doesn't put anything in the text box (code copied
from MSDN)
TextBox2.Text = row.Cells[2].Text;

//This row (when not commented out and line above commented
out) puts the row index in the text box
//TextBox2.Text = GridView1.SelectedRow.RowIndex.ToString();
}

Can anyone provide me with the code that will allow me to access the
data in each cell. When working in the debugger I'm being told that
the text value is an empty string which can't be correct coz i can see
the value in the grid. I'm new to both ASP.Net & C# so may be doing
something obviously wrong!

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top