Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Getting data from a selected row in a gridview

Reply
Thread Tools

Getting data from a selected row in a gridview

 
 
Neil
Guest
Posts: n/a
 
      11-11-2005
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

 
Reply With Quote
 
 
 
 
Fred Exley
Guest
Posts: n/a
 
      11-11-2005

"Neil" <> wrote in message
news: 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






 
Reply With Quote
 
Neil
Guest
Posts: n/a
 
      11-11-2005
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;
}
}

 
Reply With Quote
 
Fred Exley
Guest
Posts: n/a
 
      11-11-2005

"Neil" <> wrote in message
news: 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



 
Reply With Quote
 
Neil
Guest
Posts: n/a
 
      11-11-2005
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.

 
Reply With Quote
 
SueL SueL is offline
Junior Member
Join Date: Sep 2006
Posts: 1
 
      09-22-2006
Fred,

I am having the same problem as Neil.

Could I also have a copy of your app?

- many thanks in advance

Sue
 
Reply With Quote
 
chookieeee chookieeee is offline
Junior Member
Join Date: Apr 2007
Posts: 1
 
      04-17-2007
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.
 
Reply With Quote
 
Moim Moim is offline
Junior Member
Join Date: Jun 2007
Location: Dhaka
Posts: 2
Send a message via MSN to Moim Send a message via Yahoo to Moim
 
      06-07-2007
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.Contro ls[1])).Text; // if the control you defined in Item template mark up is a link button;
// otherwise you need to do the appropriate casting
 
Reply With Quote
 
m6azeez m6azeez is offline
Junior Member
Join Date: Aug 2007
Posts: 1
 
      08-08-2007
Quote:
Originally Posted by chookieeee
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

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

thanks again
 
Reply With Quote
 
andyv andyv is offline
Junior Member
Join Date: Nov 2009
Posts: 1
 
      11-25-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
displaying dropdownlist selected data in the Gridview using the objecy datasource mr.smiley ASP .Net 0 04-21-2011 12:37 PM
Get data from selected record in gridview Julia B ASP .Net 7 10-22-2008 10:51 AM
GridView with DDL and selected value mik ASP .Net 2 05-11-2008 10:13 PM
Want to add row under selected row of gridview ujjc001@gmail.com ASP .Net Building Controls 1 02-16-2007 06:14 PM
Getting data from a selected row in a gridview Neil ASP .Net 0 11-11-2005 02:57 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57