Sunday, December 6, 2009

DataGridView not updating in c#

How to update a DataGridView in c#.

I found that a DataGridView does not seem to update correctly when the DataSource is changed.

Theoretically, the following two pieces of code should be equivalent, where OutputGridView is a DataGridView, and myList is a list of objects to be displayed:

------------------------
Code 1:

OutputDataGridView.DataSource = myList;
-------------------------
Code 2:

OutputDataGridView.DataSource = null;
OutputDataGridView.DataSource = myList;
---------------------------

Given normal program semantics, the two pieces of code should be equivalent. The only way they cannot be equivalent if assignment (or specifically assignment to null) has side effects that change the semantics. This must be the case, because in y program changing the code from code 1 to code 2 changes the behavior.

And yes, I did google for solutions, the DataGridView Program Manager suggested adding

this.BindingContext[customDataTable].EndCurrentEdit();

but this did not fix the problem.

Hope that this may help someone else!

No comments:

Post a Comment