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
 
F

Fred Exley

Neil said:
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

This works for me:

//////////////////////////////////////////////////////////////
//Use this method to copy the items in the GridViewRowCollection
object
//into the specified System.Array object, starting at the specified
index.
//The System.Array object can then be used to access the items in
the collection.

// Copy the items in the Rows collection into an array.
GridViewRow[] rowArray = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(rowArray, 0);

// Iterate though the array and display the value in the first cell
of the row.
int j = -1;
foreach (GridViewRow row in rowArray)
{
j++;
if (j == idx)
{
Label1.Text = row.Cells[1].Text;
}
}
//////////////////////////////////////////////////////////////



Apparantly the key is to copy the rows into an array to be able to iterate
through it.
I got the basic idea from :
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrowcollection.aspx
-Fred
 
N

Neil

Hi Fred,

Thanks for the reply.
I used your example but still can't get anything to display.
I'm stumped

The code I used was:

// Copy the items in the Rows collection into an array.
GridViewRow[] rowArray = new GridViewRow[GridView1.Rows.Count];
GridView1.Rows.CopyTo(rowArray, 0);

// Iterate though the array and display the value in the sixth
cell of the row.
int j = -1;
foreach (GridViewRow row in rowArray)
{
j++;
if (j ==GridView1.SelectedRow.RowIndex)
{
TextBox2.Text = row.Cells[5].Text;
}
}
 
F

Fred Exley

Neil said:
Hi Fred,

Thanks for the reply.
I used your example but still can't get anything to display.
I'm stumped

The code I used was:

// Copy the items in the Rows collection into an array.
GridViewRow[] rowArray = new GridViewRow[GridView1.Rows.Count];
GridView1.Rows.CopyTo(rowArray, 0);

// Iterate though the array and display the value in the sixth
cell of the row.
int j = -1;
foreach (GridViewRow row in rowArray)
{
j++;
if (j ==GridView1.SelectedRow.RowIndex)
{
TextBox2.Text = row.Cells[5].Text;
}
}

This works on my page, displaying a specific cell of the selected row:
Label4.Text = Convert.ToString(GridView3.SelectedRow.Cells[1].Text); // Food
item desc

If that doesn't help, I can send you my complete app and you can work back
from there.

-Fred
 
N

Neil

Still can't get it to work.

I don't understand this at all. The code looks fine, the grid has data
in it and if I use this line:
TextBox2.Text = GridView1.SelectedRow.RowIndex.ToString();

I get a value displayed in the text box which indicates to me that the
code is seeing a row (at least) in the grid.

I'd be grateful for a copy of your app as I'm tearing my hair out here
trying to solve what should be a trivial matter.
 

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

Top