一个GridView绑定到一个动态或ExpandoObject对象 [英] Binding a GridView to a Dynamic or ExpandoObject object

查看:163
本文介绍了一个GridView绑定到一个动态或ExpandoObject对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用罗布科纳庞大的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.

我没有发现,建议使用一种称为即兴框架另一个#1的问题,但我不知道这会工作这一点。如果你知道它,请提供code样品实际转换的 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.

最坏的情况,有没有人实施了一个附加的方法(可共享)的大规模转换所产生的 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?

任何帮助是极大的AP preciated。谢谢你。

any help is greatly appreciated. Thanks.

推荐答案

既然你不能绑定到ExpandoObject,您可以将数据转换成一个DataTable。这个扩展方法为你做的。我会提交此列入大规模。

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绑定到一个动态或ExpandoObject对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆