Can I do this?
List<dynamic> data = _d.Query<dynamic>(_script).ToList(); // ToList() forces loading ultraGrid1.DataSource = data;
ultraGrid1.DataBind();
In the debugger, data is a list of System.Dynamic.ExpandoObjects.
Do I need to put some type of conversion in the middle?
Hello Michael,
One possible approach to solve this task could be if you are using to define the code below:
// Bindinglist of ExpandoObject
BindingList <ExpandoObject> viewList = new BindingList<ExpandoObject>();
for (inti = 0; i < 50; i++)
{
// Create Dynamic object
dynamic obj = new ExpandoObject();
// Cretae dynamic properties
obj.Item ="Test " + i.ToString();
obj.Description ="Description " + i.ToString();
obj.Comment ="Test N " + i.ToString();
viewList.Add(obj);
}
var itemsList = from p in viewList
select new
// Define grid`s column
Item = p.ElementAt(0).Value,
Description = p.ElementAt(1).Value,
Comment = p.ElementAt(2).Value
};
ultraGrid1.DataSource = itemsList.ToList();
Please take a look at the attached sample for more details and let me know if you have any questions.
Regards
I have the same problem. Dynamic types are something introduced in .NET 4.0 where we can add properties (and more) dynamically at run time.
It seems Infragistics does not yet properly support dynamic types.
You can try the following:
dynamic d = new ExpandoObject(); d.FixedName = "Hardcoded"; ((IDictionary)d)["DynamicName"] = "Arbitrary"; Console.WriteLine(d.FixedName); Console.WriteLine(d.DynamicName);
dynamic d = new ExpandoObject();
d.FixedName = "Hardcoded";
((IDictionary)d)["DynamicName"] = "Arbitrary";
Console.WriteLine(d.FixedName);
Console.WriteLine(d.DynamicName);
Hi,
This code looks fine to me. I don't see anything here that would stop this from working, unless the SomeColumn and AnotherOne properties are internal instead of public. Is this a public property?
Make sure that these are public properties on the ExpandoObject class - not just public members, they must be public properties.
I am having a hard time binding BindingList<ExpandoObject>.
Can you bind this so that it actually displays in the grid?
obj.SomeColumn = "something";
obj.AnotherOne = 42;
ultraGrid1.DataSource = new BindingList<ExpandoObject> { obj };
It creates an empty row but no columns.
Hi Guy,
Binding the grid directy to an IEnumerable won't work well at all. But copying the data into a DataTable is an excellent idea. DataTable implements IBindingList and it's made specifically for the purpose of DataBinding, so that will work very well. But you will have to define the columns in the DataTable if you want any columns to show up in the grid.
If you are resigned to copying the data into another structure, anyway, then the easiest thing to do would be to copy it into a BindingList<ExpandoObject>. You don't have to define the columns in this case, because the columns are generated automatically by the BindingList from the public members of the ExpandoObject.