GridView.SelectedRow - need Help Please

  • Thread starter Thread starter mosscliffe
  • Start date Start date
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.
 
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
 
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
 
I'd keep it to 1-3 fields if possible. We're just talking about a few values
being serialized into the viewstate, ultimately.
 
Back
Top