GridView.SelectedRow - need Help Please

M

mosscliffe

I have a gridview and in the GridView1_SelectedIndexChanged routine I
am attempting to get at the data in the row

I have done it successfully with cells

'Access the contents of a field in a GRIDVIEW
Dim joe As String = GridView1.SelectedRow.Cells(1).Text
lbxDebug.Items.Insert(0, joe)

but I thought I would try to access the data via the 'FieldName'

so I tried

Dim gvr As GridViewRow = GridView1.SelectedRow
Dim fred As TextBox = gvr.FindControl("Title")

lbxDebug.Items.Insert(0, fred.Text) but it errors with the
reference to fred.text in executing, not in compiling

Error: Object reference not set to an instance of an object.

Please help, I thought I had got this cracked.
 
G

Guest

Once you've bound a GridView, the data source does not necessarily
exist...especially if you're done a postback since then. The data is not the
control and vice/versa.

Try this, though.....
If you need access to the CustomerId and FirstName fields....
Set the DataKeyFields proeprtry to "CustomerId,FirstName"
Then....instead of having to go through your controls, check the
SelectedDataKey(s) property on the Gridview
 
M

mosscliffe

Thank you - yet another way of getting at data.

I read something about this as a means of passing hidden values to the
Grid.

Is there any performance overhead in putting several fields into the
DataKeyFields ?

Thanks again
 
G

Guest

I'd keep it to 1-3 fields if possible. We're just talking about a few values
being serialized into the viewstate, ultimately.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top