473,413 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Two way databinding with WinForms and the DataGridView

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
1 18740
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: r_elbers | last post by:
Hello, Does anybody know about some docs about real internals off databinding ? Old DDX was clear enough about when to expect what event, but it isnt very clear when the first update of a...
8
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the...
1
by: Pieter | last post by:
Hi, My problem: When the cursor is on a given row in the DataGridView, the data in the row has to be shown in TextBoxes underneath the DataGridView. the user must be able to edit in the...
1
by: John | last post by:
My columns are changing order. When I run my program the columns are in a different order than they were in design view. Is there any reason why this should happen?
1
by: =?Utf-8?B?TWF6?= | last post by:
I have a datagridview (Winforms vb.net) whose datasource is a class I have constructed. I want to allow users to edit the cells in the grid and I can't. The MS documentation states that for this to...
2
by: Jim in Arizona | last post by:
Sorry for the x-post. There seems to be very little if any activity on the windowsforms group. Nearly all my experience so far has been with web forms so I'm just learning to work with windows...
0
by: CSharpie1000 | last post by:
I am sorry for the broadness of this question, but I litterally can't get anywhere with this. I have a dataGridView that loads data from an Access 2007 Database. This works fine. I would like...
1
by: pompair | last post by:
Hi, I've been implementing a program that populates winforms DataGridView from an excel sheet (.xlsx-file). The app works ok. Now I'm refactoring it and I've got a feeling that databinding...
1
by: Andrus | last post by:
I need to create WinForms DataGridView column which shows spaces between thousands. I have found that MaskedTextBox Mask property supports this I tried to add column to DataGridView using ...
1
by: Andrus | last post by:
I have Winforms DataGridView whose DataSource Rids is Marc sample TableListCollection<TRowderived from BindingList. It is used to edit invoice rows. If user presses Revert button, my application...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.