Home
Forums
New posts
Search forums
Articles
Latest reviews
Search resources
Members
Current visitors
Newsgroups
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET
Two way databinding with WinForms and the DataGridView
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Dave A, post: 7952048"] 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: [URL="http://www.tcpiq.com/temp/DataGridView.bmp"]www.tcpiq.com/temp/DataGridView.bmp[/URL] and the source code is here [URL="http://www.tcpiq.com/temp/DataGridView.zip"]www.tcpiq.com/temp/DataGridView.zip[/URL] 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 [URL]http://msdn2.microsoft.com/en-us/library/ms184414.aspx[/URL] suffers from the same problem. Regards Dave A [/QUOTE]
Verification
Post reply
Home
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET
Two way databinding with WinForms and the DataGridView
Top