Dealing with invisible columns in a GridView when updating

When you make columns invisible in a GridView-control the values in those columns are ignored when doing an update. If the update function, stored proc, or whatever expects these values (particularly when you use CollisionDetection=”CompareAllValues”), the update will fail.


The solution:
– Add the names of the invisible columns to the DataKeyNames-properties separated by a comma, so the values are available.
– Create an event handler for the GridView.RowUpdating and add the code below. This copies all “key” values to the old and new values so the update will work.


IDictionaryEnumerator restoreOldValues = e.Keys.GetEnumerator();
while (restoreOldValues.MoveNext())

   e.OldValues.Add(restoreOldValues.Key.ToString(), restoreOldValues.Value.ToString());
   e.NewValues.Add(restoreOldValues.Key.ToString(), restoreOldValues.Value.ToString());
}

Leave a Reply

Your email address will not be published. Required fields are marked *