By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Two way databinding with WinForms and the DataGridView

P: n/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

Mar 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/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!
"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
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

Mar 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.