Bulk Edit Grid View
Nicolas Galler | November 23, 2007As planned I have implemented the bulk edit mode for the grid view.
This is very inspired from this post from Matt Dotson, in fact the only difference is that it will save automatically on every page refresh instead of relying on a save button – this allows it to work in conjunction with paging and also not be affected when postbacks occur from other controls.
Compared to his article I had to implement these additional event handlers:
- Grid.DataBinding: I call SaveGrid here to ensure the data gets saved before it is refreshed. This needs to be protected with a lock because UpdateRow may cause databinding to be performed as well
- Grid.RowUpdated: set the KeepInEditMode to true – this prevents the grid from trying to rebind (so this should remove the lock requirement from the previous handler, but I kept it in to be on the safe side)
- Grid.RowInitializing: this is an event I added to the grid, because otherwise there is no way to slip something into InitializeRow without actually deriving from the grid (my strategy with the grid view is to define feature “mix-in” modules that can be added to a stock (or in this case, “almost” stock) grid view. The problem if you derive from GridView every time is it becomes hard to combine those features… i.e. a classic problem of inheritance vs composition. Unfortunate that ASP.NET favors the former, but that is for another post)
- Grid.PageIndexChanging: call SaveGrid here otherwise the data will be muffed when the user changes pages
The SaveGrid implementation is a bit simpler, since my DataSource control is completely disconnected so I can afford to call Update pretty often… for this reason I did not bother with keeping a “dirtyRows” list and instead update every single row in the datagrid.
In the end, it gives a pretty transparent experience to the user, something like this:





