Binding a GridView to a Dynamic or ExpandoObject object

后端 未结 6 470
离开以前
离开以前 2020-12-03 03:43

I\'m using Rob Conery\'s Massive ORM, and I haven\'t been able to bind the resulting ExpandoObject to a GridView.

I did find another Stacko

6条回答
  •  攒了一身酷
    2020-12-03 04:03

    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.

    /// 
    /// Extension method to convert dynamic data to a DataTable. Useful for databinding.
    /// 
    /// 
    /// A DataTable with the copied dynamic data.
    public static DataTable ToDataTable(this IEnumerable items) {
        var data = items.ToArray();
        if (data.Count() == 0) return null;
    
        var dt = new DataTable();
        foreach(var key in ((IDictionary)data[0]).Keys) {
            dt.Columns.Add(key);
        }
        foreach (var d in data) {
            dt.Rows.Add(((IDictionary)d).Values.ToArray());
        }
        return dt;
    }
    

提交回复
热议问题