Binding a GridView to a Dynamic or ExpandoObject object

后端 未结 6 480
离开以前
离开以前 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:13

    adapting from Ben's answer

     public static DataTable ToDataTable(this IEnumerable items)
        {
            if (!items.Any()) return null;
    
            var table = new DataTable();
            bool isFirst = true;
    
            items.Cast>().ToList().ForEach(x =>
            {
                if (isFirst) 
                  {
                    x.Keys.Select(y => table.Columns.Add(y)).ToList();
                    isFirst = false;
                  }
                table.Rows.Add(x.Values.ToArray());
            });
    
            return table;
        }
    

提交回复
热议问题