Discussion Locked

Use Data in an ASP.net gridview

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
Dumb question from a newbie...
So I now have data displayed in my gridview control and I have selected the row of interest. I need to use one of the data items in the row as a parameter for my next query.

How do I refer to that data item?

strSearch = (the field in the selected row)?

Thank you.

5 replies

by tony hopkinson

selected properties....

What you get back depends on what's accessibe within the grid's column objects, or getting through to whatever the row is bound to.

but MyGrid.Sel and wait for intellisense to ping should get you on your way.

PS, you are liable to get more and better if you use Questions, rather than Discussions.

by cgreaves

Thanks Tony, this seems to get me there:

Dim selectRow As GridViewRow = GridView1.SelectedRow
Dim AddressID As String = selectRow.Cells(8).Text

I found it buried deep in MSDN someplace.
And thanks for the tip re:Questions.

by tony hopkinson

If you switch columns about or insert an earlier one, you code will fll on it's arse.
Also text in some implementations may not be exactly waht you want. For instance it could be the description of a code look up or a formatted currency value. Be really careful with dates, inherrently ambiguous formats like 01/01/99 can cause some very irritating data specific run time bugs.

Unless I'm interested in the user input while editing I tend to go underneath and get the value the grid is 'told' to display rather than 'how' it did it.

If you are using databinding you can find the row of the recordset you are bound to and access the data through that. You can see old value and new value (from user edit) as well.

Then you are using MyCurrentRow[MyCurrentField]. So changes in presentation will not affect your logic by default.

by cgreaves

I appreciate your points. In fact, I ran into that very issue when trying to use the text from the gridview to retrieve a new record with an integer primary key. I'm now using the name and gridvew1.selectedvalue. as the parameter for the next query.

I didn't realize it until I read your reply, but I can now change the columns around with impunity!
Thank you
Chris

This discussion is closed to new replies.