Reputation: 38555
Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit: What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
Upvotes: 1
Views: 1145
Reputation: 8055
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want
Upvotes: 1
Reputation: 377
I'm guessing DataItem is only properly filled when you are using DataBinding. Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our data controls) does not save data items across postbacks. This reduces ViewState (if the objects are even serializable) and enables garbage collection to happen and clean up your objects. So, when you click the button to post back, the GridView has not called DataBind and therefore your data item isn't there. This is what you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items. I've always used that and it worked. msdn about SelectedValue
Upvotes: 5