Search Here

Custom Search

Friday, August 17, 2007

An interesting bug of DataGrid of .NET

When I was working at Uniqa S&S Ltd. I got a very interesting bug of .NET datagrid.
Suppose you have bound a Dataview to a Datagrid, then what do u expect?
You expect that the changes that you make to ur DataView will be reflected to the DataGrid immediately. But sometimes it doesn't and it will really make u tensed. But how?? Ok, let me expalin.
To bind a DataView to a DataGrid you just write the following lines of code.
DataGrid dg;
DataView dv;
dg.DataSource = dv;
But sometimes when insert rows in the DataView or make any changes you don't see the reflection in the DataGrid. But when this scenario occurs?
I observed this happens when you bind more than a ComboBox or a DateTimePicker with any column of the DataView's Table.
But I found a solution of this disgusting problem.
Just write following lines after making change to the DataView.
dg.DataSource = null;
dg.DataSource = dv
Then you will see the reflection immediately.

But this is a funny solution. Because the DataSource of the dg was dv from the very beginning and still it is. But to get the reflection you have to change the datasource of the dg and again assign to ur dv. Null is a better choice here.

And the interesting fact is that MS hasn't solve this bug yet in .NET2.0
DataGridView of .NET2.0 has still the same problem.
(Note: I observed this phenomena in desktop development and I am not sure about Web)

No comments: