Databinding to a single object instead of a collection
Hello
I have read that deriving a class from BindingList(Of Type) and implementing the IComponent interface, allow us to bind a grid to a custom object collection at design time.
Now, I would like to know what is the necessary approach when the intention is to databind to a single object, not a collection.
For example, suppose that I have an Order object, which has a Lines Property that return a LineCollection object. The LineCollection correspond to the Detail of the Order and I should be able to bind it to a grid; and the object Order itself has properties of its own that I would like to bind to Textboxes, for instance. But I think that deriving Order from BindingList is an overkill; so what should I do?
Thanks a lot.
Report post as abusive
Re: Databinding to a single object instead of a collection
Quote:
Originally Posted by EMoscosoCam
I have read that deriving a class from BindingList(Of Type) and implementing the IComponent interface, allow us to bind a grid to a custom object collection at design time.
Where did you read that? I hope you're not talking about my Custom Collection thread because I certainly didn't say that. IComponent has nothing whatsoever to do with data-binding. The purpose of IComponent is to provide design-time support for a class. Any class that implements IComponent can be opened in the designer and can be added to a form in a designer. That might allow you to bind an object to your UI at design time instead of in code but it has nothing specifically to do with data-binding itself.
Speaking of my Custom Collection thread, if you haven't read it you might like to follow the link in my signature. You may find it informative.
As for your question, there's no difference between binding to an object and binding to a list. You simply do this:
vb.net Code:
myControl.DataBindings.Add(controlPropertyName, source, sourcePropertyName)
If the source is a list then the sourcePropertyName will be the name of property of the items in that list. If source is not a list then sourcePropertyName will be the name of a property of the source itself.
Note that, for the purposes of the preceding paragraph, the term "list" refers to any object that implements the IList or IListSource interface.
Re: Databinding to a single object instead of a collection
Hello
I meant that you need to do both things in order to Databind and to do it so at design-time.
I read the following article:
http://msdn.microsoft.com/en-us/magazine/cc163745.aspx
Thanks a lot.
Re: Databinding to a single object instead of a collection
Quote:
Originally Posted by EMoscosoCam
Hmmm... I'll have a good read of that myself.