Reputation: 453
I have an ASP.NET 4.5 application that is used a Telerik multi-column Combo box which is data-bound.
The issue I have is that it does not have a valid items array like a normal Telerik combo-box or asp.net combo-box, instead one has to retrieve the data from the DataSource property which is of type object
.
Given the following initialization:
class bob
{
Guid id { get; set; }
String name { get; set; }
String tel { get; set; }
}
List<bob> bobarray = .... // some data
MyCombo.DataSource = bobarray;
MyCombo.DataBind();
I am unable to cast MyCombo.DataSource
back into a List<bob>
. Instead I get an anonymous type <>f_AnonymouseType12
3[[System.Guid, System.String, System.String`
I can cast the DataSource to an IEnumerable<object>
, but I cannot get to the individual properties or cast it back to its original type List<bob>
.
var bobarray = ((IEnumerable<object>)MyCombo.DataSource).ToList();
but bobarray
is still of type List<object>
, attempting to cast to List<bob>
or IEnumerable<bob>
throws an exception "specified cast is not valid".
What I am trying to do is search for an item in the multi-column dropdown that has a matching id, then using its index (bobarray.FindIndex()
) set the item as selected if it is found.
If I do bobarray[0].ToString();
, then I can see the anonymous type properties (id, name, tel) with values, but is is a string not a type.
Does anyone know how to correctly cast the item, or find the index of an item in a Telerik radmulticolumncombobox in ASP.NET 4.5?
Upvotes: 0
Views: 63
Reputation: 453
In answer to my own question, I ended up writing an extension method that uses reflection. It takes two types (T and S) for target and source, and then just copies properties that have the same name (sourceProp.GetValue() and the targetProp.SetValue()).
I also had to put in a check for the source being IEnumerable.
Upvotes: 0