473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 PropertyNameCha nged pattern OR the new
INotifyProperty Changed 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 RaiseListChange dEvents 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 18797
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**@sigmasolu tionsdonotspamm e.com.au> wrote in message
news:u7******** ******@TK2MSFTN GP09.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 PropertyNameCha nged pattern OR the new
INotifyProperty Changed 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 RaiseListChange dEvents 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
1381
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 databinding,once defined is triggered. Ofcourse if you go form_load everything is done, but for instance in my testcode I dont want to show anything,...
8
26394
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 datagridview control? I am at a loss. Thanks, David
1
1632
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 Datagridview, or in the textboxes. The text in these textboxes and the datagridview has to be synchronsied at all time. So if the user types "a" in the...
1
2070
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
4648
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 be done the underlying data source should support editing. (I have enabled the grid, set the EditMode property and ReadOnly property...
2
1424
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 forms. I'm trying to bind data do a datagridview but dont' know how. This is what I have so far. ========================================
0
1245
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 to, when I double click on a dataGridView row though, have a second form open with the information displayed in textboxes, etc. Not all of my data is...
1
3266
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 could be done more elaborately than I'm doing currently. First I read in the excel file and extract the first row and make it's columns the header...
1
9731
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 var column = new DataGridViewMaskedTextBoxColumn(); column.Mask= "99 999 999.99"; but got error because DataGridViewMaskedTextBoxColumn does not...
1
2808
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 reads original rows from server database: List<TRowtr; tr = q.Where(r =r.Dokumnr == Dok.Dokumnr) .OrderBy(r =r.Reanr).ToList(); Rids = new...
0
7408
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7661
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7815
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7433
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7763
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5340
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1891
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.