Monday, June 04, 2007

The amount of help you can find on the web about binding a datasource to a datagrid is astounding. Once you bound your datagrid and populated it, the user of course wants to see it.

They may optionally want to update, delete or insert data into your nicely bound datagridview.

All of the above is well documented on the web and easy to pull off. So now what?

Let's suppose you want to "Do" something with the data, say.... save it back to a source?

Well if you were using an unbound datasource and populated it on the fly, AND thought you could persist some data in hidden columns, you are sadly mistaken.

The data in hidden columns is never bound on the databind(). That’s right, a predefined column with the datafield attribute set and the visible attribute set false will not bind to the datasource datafield.

The trick is to define all columns of data you wish to persist to visible columns and then after you call the databind(), hide the columns you don't want to show the user. The data will now persist.

CAUTION! If you rebind (update the datagrid datasource) after you've hidden the columns, the data will be lost again. You must first make the columns visible again, then rebind the datagrid and then you may hide the columns once more.