Dec 03, 2007 8:39 AM
RowDeleting handler for my
GridViewDeleteEventArgs.Keys collection is empty. I'm setting the
DataKeyNames property correctly - what gives?
I found a number of posts about this issue - the DataKeys are populated by the
DataBind() but don't seem to survive postback. But they do survive the postback - they just aren't passed
GridViewDeleteEventArgs. The keys are still in the GridView itself after postback.
So instead of this:
int ID = int.Parse(((DictionaryEntry)e.Keys[e.RowIndex]).Value.ToString());
I just do this:
int ID = (int)gvSessions.DataKeys[e.RowIndex].Value;
I saw a couple of suggestions to re-bind the data right before doing the delete. This of course ignores the possibility that the data has changed. The wrong record could wind up being deleted, if you're just going by index in the GridView.