First of all I have a datasource in my winforms project where I needed to bind the text in a checkedlistbox to a nested property in this case the “Name”
object[0] –> type of ILink –> Name = “adobe”
object[1 ]–> type of ILink –> Name = “flash”
To accomplish this I found a smarter guy here who led me to a structure like with the relevant code in the GetView() method.
The problem is that this doesn’t work for a webcontrol , GetView() doesnt get called. So I’d love to know what modifications I would need to support a webcontrol using this method.
public class ILinkCollection : List<ILink>, ITypedList
{
}
public class ILinkProgramView : IILinkViewBuilder
{
public PropertyDescriptorCollection GetView()
{
List<PropertyDescriptor> props = new List<PropertyDescriptor>();
IProgramDelegate del = delegate(ILink d)
{
return d.Program.Name;
};
props.Add(new ILinkProgramDescriptor("FullName", del, typeof(string)));
del = delegate(ILink dl) { return dl.IsActive; };
props.Add(new ILinkProgramDescriptor("IsActive", del, typeof(string)));
PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];
props.CopyTo(propArray);
return new PropertyDescriptorCollection(propArray);
}
}
public class ILinkProgramDescriptor : PropertyDescriptor
{
}
I then set the datasource like so
ILinkCollection iLinkPrograms = new ILinkCollection(new ILinkProgramView());
clbProgs.DataSource = iLinkPrograms;
clbProgs.DisplayMember = "FullName";
clbProgs.ValueMember = "IsActive";
Web data-binding isn’t against
IList(and henceIListView,ITypedList, etc), but ratherIEnumerable. That would still be fine (the list isIEnumerable), howeverDataBinder.GetPropertiesFromCacheworks on a per-object level viaTypeDescriptor.GetProperties(obj)– meaningITypedListdoesn’t get a look-in.So, to do this you will need to either implement
ICustomTypeDescriptoron the items, or write aTypeDescriptionProvider. Both are tricky. And because it is talking about a singleobject, it isn’t even going to noticeILink– you’ll need to do this for the concrete implementation(s).Or easier; just map to a simple class (view-model) to represent your data – it’ll save you a lot of time.