Tutorial :How do I build control that determines property value based on bound DataSource?



Question:

I have a database of people and shirts. Each person has specific shirts that he likes to wear. For this, I have three tables. Person, Shirt, and PersonShirt. PersonShirt consists of a list of rows with a PersonId and ShirtId. For UI, I'm using a CheckedComboBox. This is a combo box of checked items. For a specific person, the combo box lists all of the shirts and the items are checked or unchecked depending what he likes to wear. If an item is checked, a new PersonShirt is added to the database.

This is just an example of a scenario that comes up several times within one of our company's projects. So, I created a custom CheckedComboBox that takes in a LinksDataSource for the "PersonShirt" and a LinksValueMember for the "ShirtId."

How can I get a property, based on the LinksValueMember string, from a specific item in the LinksDataSource? LinksDataSource is a BindingSource.


Solution:1

I believe that the correct way to do this is to use the ITypedList interface. If the bound list implements ITypedList, call ITypedList.GetItemProperties(null) to get the properties, and find the property that you want the value of. Then use the resultant PropertyDescriptor to get the value:

ITypedList typedList = this.dataSource as ITypedList;  PropertyDescriptor valueDescriptor = typedList.GetItemProperties(null)).FirstOrDefault(d => d.Name == this.linksValueMember);  object value = valueDescriptor.GetValue(listObject);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »