Since beginning to use VB.NET some years ago I have become slowly familiar with using the data binding features of .NET, however I often find my self bewildered by it's behavior and instead of discover the correct way it should work I find some dirty work around to suit my needs and continue on.
Needless to say my problems continue to arise.
I am using Custom Objects as the Data Sources for by controls and often entire forms. I find it frustrating to separate business logic and the graphical interface. (That could be a new question entirely.) So for a lot of objects I generate a form which has the DataBindingSource
for the object. When I create each from using the New Constructor I explicitly pass to it the object to which it should be bound, and then set this passed object as the DataSource
of the BindingSource
. (That's a mouthful!)
Now the Master object (say, bound to each form) often contains a List of objects which I like to have displayed in a DataGridView
. I (sometimes) create and modify these child objects in their own form (again creating a databind the same way as the master form) but when I add them to the List in the master object the DataGridView
won't update with the new items.
So my question really 开发者_StackOverflowhas a few layers:
How can I easily/efficiently/correctly update this
DataGridView
with the list of Detail objects when I add them to the list of the Master object.Is this approach to
DataBinding
good/viable.What's the best way to separate business logic from graphical interface.
1) use BindingList<T>. it handles all of the refreshing of the gridview for you. i've used this many times, and i love it. it's super easy and does what you want. http://msdn.microsoft.com/en-us/library/ms132679.aspx
2) yes
3) Model-View-Presenter pattern with a ViewModel or PresentationModel. there's a lot of great info on Model-View-Presenter out there on the web. read up on it and learn it well. and by the way: you'll see a lot of MVP info related to web development with asp.net webforms... it works just as well in WinForms development. I've been using MVP in winforms for years.
精彩评论