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.
 
Joined
Sep 22, 2006
Messages
1
Reaction score
0
GridView: retireving selected row data

Fred,

I am having the same problem as Neil.

Could I also have a copy of your app?

- many thanks in advance

Sue
 
Joined
Apr 17, 2007
Messages
1
Reaction score
0
after taking a good 2.4 hours of my day,
i worked it out... altho i dont know why this works...

string s = GridView1.SelectedRow.Cells[2].Text;

the '2' seems to give me my first column... i have no idea why.

ow well. it works. have fun.
 
Joined
Jun 6, 2007
Messages
2
Reaction score
0
I guess, the best thing to do this is as follows

For example if you have a row where the first column is a link button into the item template then your approach should be

Control ctl = row.Controls[0];// get the first control reference
string value = ((System.Web.UI.WebControls.LinkButton)(ctl.Controls[1])).Text; // if the control you defined in Item template mark up is a link button;
// otherwise you need to do the appropriate casting :)
 
Joined
Aug 8, 2007
Messages
1
Reaction score
0
chookieeee said:
after taking a good 2.4 hours of my day,
i worked it out... altho i dont know why this works...

string s = GridView1.SelectedRow.Cells[2].Text;

the '2' seems to give me my first column... i have no idea why.

ow well. it works. have fun.


you are the best :flute: :flute:

thanks a lot dude I like your simple and correct way of solving this issue
it works perfectly

thanks again
 
Joined
Nov 25, 2009
Messages
1
Reaction score
0
a way round this problem is to get the keydata from the gridview:

inside the gridview tag include the field you want to access in this field:

Code:
<asp:GridView ID="grdCalls" runat="server" DataKeyNames="FolderID, FolderName">

then in the event method:

Code:
grdCalls_SelectedIndexChanged(object sender, EventArgs e)
{
string FolderID = grdCalls.SelectedDataKey[0].ToString()

}

should do the trick nicely
 
Joined
Jan 19, 2011
Messages
1
Reaction score
0
Solution to problem if using item templates in gridview

SelectedRow.Cells[] function does not work if you have template fields in your gridview.

Solution to problem if using item templates in as gridview columns:

Label lbl = (Label)GridView1.SelectedRow.FindControl("Label1");
string str = lbl.Text;


Also, it was SelectedRow.Cells[2] because cells[0] was probably an edit cell and cells[1] was probably a delete cell or vice versa.
 
Joined
Feb 8, 2012
Messages
1
Reaction score
0
protected void gvInventory_SelectedIndexChanged(object sender, EventArgs e)
{
//int orderID = (int)this.gvInventory.SelectedRow.Cells.Count;

GridView dummyinv = new GridView();
//int rowval = gvInventory.Rows.Count;
GridViewRow row = gvInventory.SelectedRow;
//string name = row.Cells[0].Text;
//select the row count value
string name = gvInventory.SelectedRow.RowIndex.ToString();
query = "select TireCompany,TireTreadType,TireSize,TireTreadDepth,TireTreadDepthC,Price,TireDiameter,TireCircumference,Ref,GoneDate,Customer from TblTireInventoryDetails where TireCompany=@cname";
OleDbCommand cmd = new OleDbCommand();
cmd.Parameters.AddWithValue("@cname", name);
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.Connection = conn;
try
{
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
while (dr.Read())
{
ddlCompany.SelectedItem.Text = dr[0].ToString();
ddlTreaddesign.SelectedItem.Text = dr[1].ToString();
ddlTiresize.SelectedItem.Text = dr[2].ToString();
txtOTDepth.Text = dr[3].ToString();
txtCTDepth.Text = dr[4].ToString();
txtPrice.Text = dr[5].ToString();
txtDiameter.Text = dr[6].ToString();
txtCircumference.Text = dr[7].ToString();
txtRef.Text = dr[8].ToString();
ddlDate.SelectedItem.Text = dr[9].ToString();
ddlCompanyout.SelectedItem.Text = dr[10].ToString();
//txtFax.Text = dr[11].ToString();
//txtPassword.Text = dr[12].ToString();
//break;

}
dr.Close();
}



}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}

}


i think its very usefull to you..........
P.Magesh
 
Joined
Apr 24, 2012
Messages
1
Reaction score
0
Thank You

chookieeee thank you so much i spent like 4 or 5 hours trying to solve this problem really
THANK YOU:congrats:
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top