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