views:

149

answers:

2

I want to databind to a collection property, such as Count.

in general, when I data bind I specify data member for the object property in the collection, not for the actual properties collection itself exposes.

for example, I have a list of custom objects. I show them in datagridview. but I also want to show their total count using a separate label. is there a way to do this through databinding?

I imagine that somehow I need to force PropertyManager to be used, instead of CurrentyManager?

I get the following exception. Notice that DataSource is collection and has TotalValue property.

System.ArgumentException: Cannot bind to the property or column TotalValue on the DataSource.
Parameter name: dataMember
   at System.Windows.Forms.BindToObject.CheckBinding()
   at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
   at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
   at System.Windows.Forms.BindingsCollection.Add(Binding binding)
   at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
   at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
   at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
   at System.Windows.Forms.BindingsCollection.Add(Binding binding)
A: 

I don't think there's any reason you can't bind, say, a TextBox to myObjectList.Count, but myobjectList will need to implement IBindingList<T> either on its own if it's a custom collection with additional validation or by virtue of being of type BindingList<T>. IBingingList<T> contains events that fire when an object is added, removed, or changed within the collection.

See the MSDN Docs on BindingList and IBindingList. You'll probably want to use the former since the latter has a lot of members to implement.

Josh Kodroff
@Josh Kodroff, actually myobjectList is a BindingList. I can bind to the properties of the items in the collection, but not to the property of the actual list (such as Count). for TextBox, I can specify property of the item in the collection and TextBox gets bound to the 1st item in the collection. When I try to bind to the List property I get an exception.
Ornus
Can you add the exception you get to your post?
Josh Kodroff
@Jesh Kodroff: added exception text
Ornus
A: 

Yes,I do it!I can specify property of the item in the collection and TextBox gets bound to the 1st item in the collection.

star