Search code examples
c#vb.netwinformsbindingsource

Can I get the item type from a BindingSource?


I would like to get the Type of item that a BindingSource is hooked up to or configured for. The BindingSource.DataSource property can be set to an object, list, or type. If it is a Type, it obviously does not have a bound item yet, but I would still like to get the Type. For a List, I need the item Type, not the list type.

I currently have a custom list type for business objects that implement an IListItemType interface, that I created to solve this problem a while back. I would now like to get this working in a more generic fashion so that it will work with any list.

I've looked through the API docs for for a good way to do this, but so far I have not had any luck. Am I missing something or is this just something I can not or should not be doing?


Solution

  • I recently ran across the ListBindingHelper class in the framework that has everything I was looking for:

    System.Windows.Forms.ListBindingHelper.GetListItemType()
    

    Returns the type of an item contained in a list or collection.

    ListBindingHelper.GetListItemProperties()
    

    Returns a PropertyDescriptorCollection that describes the properties of the items contained in the list.