如何将<string,object =“”>字段的集合绑定到GridControl WPF?

时间:2019-02-25 00:03:19

标签: c# .net wpf collections ado.net

我搜索的是Google社区,所以我在搜索和挖掘后问了这个问题

我有一组在运行时填充的列和值,因此我想从“字段收集”中填充GridControl,但我不想使用DataTable或任何ADO.NET。仅.net集合。 我也不想使用具有固定属性的固定类,例如Employee类并列出Employee。 不,我不需要

我只需要在运行时从我的自定义字段列表或字典中创建GridControl WPF控件的列和值

List<Field> Fields {get;set;}

public class Field{
   public string ColumnID{get;set;}
   public object Value{get;set;}
}

处于测试阶段

Field field1 = new Field();
field1.ColumnID = "branchID";  // this column id at runtime i got it
field1.Value = 100;

Field field2 = new Field();
field2.ColumnID = "customerID"; // at runtime but this for example
field2.Value = 1; // at runtime but this for example

Field field3 = new Field();
field3.ColumnID = "Runtime-ColumnID" ;
field3.Value = RuntimeValue;

Fields.Add(field1);
Fields.Add(field2);
fields.Add(field3);

请如何归档此文件:

  • 我不想将DataSet与GridControl一起使用
  • 我不想将Datatable与GridControl一起使用
  • 我不想将ADO.NET与GridControl一起使用
  

i我只想从字段集合中填充GridControl。任何   像LINQ这样的建议,动态绑定将得到重视。

1 个答案:

答案 0 :(得分:0)

请查看以下代码;

注意:由于时间有限,我已使用Newtonsoft json将最终对象(ExpendoObject)转换为匿名类型(因为expendo对象无法绑定到网格)。您可以改进代码。

此外,我在Field类中添加了附加属性“ RowNumber”,它将指示程序映射特定行中的列。

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        List<Field> fields = new List<Field> {
            new Field{ ColumnID="Name", Value="Kamran" , RowNumber=1},
            new Field{ ColumnID="Age", Value=25 , RowNumber = 1},
            new Field{ ColumnID="Name", Value="Asim", RowNumber = 2, },
            new Field{ ColumnID="Age", Value=30 , RowNumber = 2 },
            new Field{ ColumnID="Department", Value="Computer" , RowNumber = 2},
        };

        var result = fields.ToPivotArray(item => item.ColumnID, item => item.RowNumber, items => items.FirstOrDefault()?.Value);

        string json = JsonConvert.SerializeObject(result, new KeyValuePairConverter());
        var obj = JArray.Parse(json);
        testGrid.ItemsSource = obj;
    }
}

public static class Extension
{
    public static dynamic[] ToPivotArray<T, TColumn, TRow, TData>(this IEnumerable<T> source, Func<T, TColumn> columnSelector,
                                                                   Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
    {
        var arr = new List<object>();
        var cols = new List<string>();
        String rowName = ((MemberExpression)rowSelector.Body).Member.Name;
        var columns = source.Select(columnSelector).Distinct();
        //cols = (new[] { rowName }).Concat(columns.Select(x => x.ToString())).ToList();
        cols = (columns.Select(x => x.ToString())).ToList();
        var rows = source.GroupBy(rowSelector.Compile())
                         .Select(rowGroup => new
                         {
                             Key = rowGroup.Key,
                             Values = columns.GroupJoin(
                                 rowGroup,
                                 c => c,
                                 r => columnSelector(r),
                                 (c, columnGroup) => dataSelector(columnGroup))
                         }).ToArray();

        foreach (var row in rows)
        {
            var items = row.Values.Cast<object>().ToList();
            // items.Insert(0, row.Key);
            var obj = GetAnonymousObject(cols, items);
            arr.Add(obj);
        }
        return arr.ToArray();
    }
    private static dynamic GetAnonymousObject(IEnumerable<string> columns, IEnumerable<object> values)
    {
        IDictionary<string, object> eo = new ExpandoObject() as IDictionary<string, object>;
        int i;
        for (i = 0; i < columns.Count(); i++)
        {
            eo.Add(columns.ElementAt<string>(i), values.ElementAt<object>(i));
        }
        return eo;
    }

}

public class Field
{
    public string ColumnID { get; set; }
    public object Value { get; set; }
    public int RowNumber { get; set; }
}