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?
Guy__L said:Can I do this?
Yes, you can do this. The grid will bind to a List<t>. But a List is not a great data source to use, since it's very limited in the notifications it sends.
If you use a List, there are certain things that the grid may be unable to do, such as add new rows to the data. The grid probably won't be notified of certain changes to the List, also, such as when you change the value of a property on your ExpandoObject.
A BindingList<T> is generally a better data source to use.
But... if you are just displaying the data and you don't need a lot of robust support for adding rows or changing the data outside of the grid, then this code should be fine.
Thank you for your answer Mike.
The following code is what worked for me (novice in UltraGrid that I am):
var data = _d.Query<dynamic>(_script); // returns IEnumerable<ExpandoObject>
if (data == null || data.Count() == 0) {
ultraGrid1.Text = "No records found";
return;}
IDictionary<string, object> c = (IDictionary<string, object>)data.FirstOrDefault();
DataTable dt = new DataTable();
dt.BeginLoadData();
dt.Columns.AddRange(c.Keys.Select(k => new DataColumn(k)).ToArray());
foreach (IDictionary<string, object> r in data)
dt.Rows.Add(r.Values.ToArray());
dt.EndLoadData();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ultraGrid1.Text = data.Count() + " record" + (data.Count() == 1 ? "" : "s") + " found";
ultraGrid1.DataSource = ds;
Doing it the first way I asked gives me the rowcount, but I don't think it adds any columns, because the grid is still blank.
Regards,
Guy
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.
I am having a hard time binding BindingList<ExpandoObject>.
Can you bind this so that it actually displays in the grid?
dynamic obj = new ExpandoObject();
obj.SomeColumn = "something";
obj.AnotherOne = 42;
ultraGrid1.DataSource = new BindingList<ExpandoObject> { obj };
It creates an empty row but no columns.
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 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);
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
// 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