Two way databinding with WinForms and the DataGridView

D

Dave A

Hi,



I am struggling with two way databinding in WinForms and the DataGridView.
I am binding to business object classes (rather than datatables). If I
have a collection of these business objects that is the datasource of a
DataBinding that is bound to a DataGridView (WinForms) then I was expecting
that any change to the data would be reflected in the DataGridView (in other
words I was expecting the 2 way databinding to actually work).



The recipe seems quite simple:



1. Create a class (we will call it Stuff)

2. Add some private members with get/set properties

3. Implement the PropertyNameChanged pattern OR the new
INotifyPropertyChanged but not both

4. Create a StuffCollection (public class StuffCollection : List<Stuff>)

5. Create some Stuff and add it to the StuffCollection

6. Add a DataGridView to a form

7. Set the bindsource's RaiseListChangedEvents to true (a trap for
beginners)

8. Add a BindingSource and set its datasource to the StuffCollection

9. Set the DataGridViews datasource to the bindingsource

10. And presto - it should all work.



.. and it kind of does except .



1. I have a button on the screen that changes some data in the last element
of the StuffCollection. The change is only reflected in the DataGridView
when that row is selected OR the insertion row is selected. When any other
row is selected the changes are not reflected in the UI. I would have
expected that any changed in the StuffCollection to be reflected in the
DataGridView.



2. If a new row is added to the StuffCollection (through code, not the UI)
then the UI is not updated to reflect this new row. When the insertion row
is then selected in the DataGridView an exception is thrown.



3. If a row is deleted from the StuffCollection (through code, not the UI)
then the UI is not updated to reflected this deleted row. Then the row that
has been deleted is selected then an exception is thrown.



I have a small screen shot of a sample application here:
www.tcpiq.com/temp/DataGridView.bmp and the source code is here
www.tcpiq.com/temp/DataGridView.zip



To reproduce the issues,



1. Select the first row and click "Change last fax number". Nothing
happens. Click the third row and click "Change last fax number". The fax
number changes. Click the fourth row (the insertion row) and click "Change
last fax number". The fax number changes. Surely this is not the correct
behavior?



2. Select the first row. Click "Add new row"; this will add a new item to
the collection. Nothing happens in the UI. Select fourth row. Marvel at the
exception. Surely a new row should appear from the UI when it is added to
the collection?



3. Select the second row. Click "Delete First Row". Nothing happens in the
UI. Select the first row. Marvel at the exception. Surely the first row
should disappear from the UI when it is deleted from the collection?



What am I doing wrong? Is two-way data binding more like one-and-a-half-way
data binding?



Coincidently, the example at
http://msdn2.microsoft.com/en-us/library/ms184414.aspx suffers from the same
problem.



Regards

Dave A
 
D

Dave A

Solved my own problem.

IList does not implement IBindingList. It is the other way around. If your
collection implement BindingList<T> then it works much better!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top