Binding a GridView to a Dynamic or ExpandoObject object

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

    I came to this thread after researching on the same topic, I found no solution but constructed my own as I desperately needed this one. So here is my solution without going POCO that really works.

    // Fill "list" with your dynamic objects.
    
    // cast a dynamic object to dictionary so we get the properties from it.
    var props = list[0] as IDictionary;
    
    // create a datatable 
    var table = new System.Data.DataTable();
        foreach (var c in props.Keys)
            table.Columns.Add(new DataColumn(c));
    
    foreach (var o in list)
    {
        var row = table.NewRow();
        var op =  o as IDictionary;
        foreach (var p in op.Keys)
        {
            row[p] = op[p];
        }
        table.Rows.Add(row);
    }
    

    And simply bind this table to your grid!

    I tested it and it worked for me.

提交回复
热议问题