将 GridView 绑定到 Dynamic 或 ExpandoObject 对象 [英] Binding a GridView to a Dynamic or ExpandoObject object
问题描述
我正在使用 Rob Conery 的 Massive ORM,但我无法将生成的 ExpandoObject
绑定到 GridView
.
I'm using Rob Conery's Massive ORM, and I haven't been able to bind the resulting ExpandoObject
to a GridView
.
我确实发现了另一个 Stackoverflow 问题,该问题建议使用名为 impromptu 的框架,但我不确定这是否适用于此.如果您知道这样做,请提供一个代码示例,将 ExpandoObject
实际转换为 GridView
控件可以绑定到的内容.
I did find another Stackoverflow question that suggests using a framework called impromptu, but I'm not sure if that would work for this. If you know it does, please provide a code sample to actually convert an ExpandoObject
to something that the GridView
control can bind to.
最坏的情况,是否有人为 Massive 实现了一种额外的方法(可以共享)以将结果 ExpandoObject
转换为 POCO?
Worst case scenario, has anyone implemented an additional method (that can be shared) for Massive to convert the resulting ExpandoObject
to a POCO?
非常感谢任何帮助.谢谢.
any help is greatly appreciated. Thanks.
推荐答案
由于无法绑定到 ExpandoObject,因此可以将数据转换为 DataTable.这个扩展方法会为你做到这一点.我可能会将其提交给 Massive.
Since you can't bind to an ExpandoObject, you can convert the data into a DataTable. This extension method will do that for you. I might submit this for inclusion to Massive.
/// <summary>
/// Extension method to convert dynamic data to a DataTable. Useful for databinding.
/// </summary>
/// <param name="items"></param>
/// <returns>A DataTable with the copied dynamic data.</returns>
public static DataTable ToDataTable(this IEnumerable<dynamic> items) {
var data = items.ToArray();
if (data.Count() == 0) return null;
var dt = new DataTable();
foreach(var key in ((IDictionary<string, object>)data[0]).Keys) {
dt.Columns.Add(key);
}
foreach (var d in data) {
dt.Rows.Add(((IDictionary<string, object>)d).Values.ToArray());
}
return dt;
}
这篇关于将 GridView 绑定到 Dynamic 或 ExpandoObject 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!