Question from a reader about DataBindings
A few questions and some answers about how to best perform data binding when you create windows forms.Question: (or actually a few questions. Answers are inline)
I'm an up-and-coming .net developer whose trying to implement databinding to business objects. Your article here has been invaluable. It's the shortest, sweetest, thing I've found to get me up to speed. And I'm happy to say I've got a lot of things working. There's two questions I was hoping you could answer for me though, if you have a moment.
Other than precluding non-serialized events (thanks to Lhotka for pointing this out to me) is there any reason not to use VB.net versus C#? My company prefers VB so that's the route I need to go, unless there's a reason I shouldn't.
Answer: No, there really isn’t much difference. In fact, my favorite resource for Windows DataBinding is Chris Sell’s book. It’s available in both VB.NET and C# versions.
Is it just me, or is databinding to multiple controls finicky? I find your sample (linked above) works fine with just a datagrid. And I am usually able to add textboxes also, binding to the same collection at the same time. But it seems to blow up sometimes. I'm not exactly sure what causes it. But I thought maybe off the top of your head, you would know if this should even work at all?
Answer: Once again, Chris Sells discusses this. The key is that you must have the same datasource for all the controls. If you do that correctly, the CurrencyManager will keep the different controls in synch. However, if you have different datasources, you get different Binding Managers. Different Binding Managers means different binding contexts, which means that the different controls do not stay in synch when the Current index changes. My suggestion is to look at your code and make sure you use the same object for the DataSource in all cases. The DataMember is what you change to get different properties in each different control.
The original articleA discussion of databinding and multiple controls
Chris Sell's book The C# Version
Chris Sell's bookThe VB.NET Version
Rockford Lhotka's blogAnother great resource