473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting data from a selected row in a gridview

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

Nov 19 '05 #1
4 35600

"Neil" <ne*********@synapse-partnership.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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/lib...ollection.aspx
-Fred


Nov 19 '05 #2
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;
}
}

Nov 19 '05 #3

"Neil" <ne*********@synapse-partnership.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 19 '05 #4
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.

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Neil | last post by:
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...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
2
by: | last post by:
I want to know how to make a clickable button or Command field on a GridView, and have the user's action a) fire a function and b) pass a data value from one of the GridView's columns to that...
1
by: MikeB | last post by:
ooh boy, I hope I'm in the right place to ask this. I'm trying to do a class project that binds controls to data sources. I have a Drop-down List that I bound to the Author column of an SQL...
0
by: GMartin | last post by:
I have a pop-up form with a three columned Grid that has checkboxes in a Template Column in the first/left-most column. (The form is to allow users to select "Members" of a group, where they check...
2
by: Steve Kershaw | last post by:
Hello, I'm trying to bind a DirectoryInfo array to a GridView (.DataBind();). Whenever I hit the gridDirList.DataBind(); method I get the error: A field or property with the name...
3
by: David C | last post by:
I have a GridView and when the SelectedIndexChange occurs I want to put the value of a BoundField into a TextBox (txtPropertyID) on the page. Below is the code I am using but it is not getting the...
2
by: wildman | last post by:
RE: Gridview textbox has data check without postback.. javascript? I have a gridview with a textbox. I can set the textbox to autopostback and check for a value in a prerender event to decide if...
0
by: jaredciagar | last post by:
Hi Guys, Can You Help Me PLease, I'm Currently facing Problems in my system... I need some help... I'm using VB script,ASP.net,MS SQL Server2005 I want to select a specific data in...
7
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi all, this is a second post, so apologies, but I never had an answer to my first post (several weeks ago) and I really need some help. I'm using a .Net 2.0 Gridview which is populated using an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.