简体   繁体   English

如何绑定的集合 <string, object> GridControl WPF的字段?

[英]How to bind a Collection of <string, object> Fields to GridControl WPF?

I search google, community, so i asked this question after searching and digging 我搜索Google,社区,因此我在搜索和挖掘后问了这个问题

I have a collection of columns and values that fill at run-time, so that i want to fill GridControl from Collection of fields but i don't want to use DataTable or any ADO.NET things. 我有一个在运行时填充的列和值的集合,因此我想从字段集合中填充GridControl,但我不想使用DataTable或任何ADO.NET东西。 only .net Collections. 仅.net集合。 also i don't want to use fixed class with fixed properties like class Employee and make a list of Employee. 我也不想使用具有固定属性的固定类,例如Employee类并列出Employee。 no i don't need that, 不,我不需要

I just need to create columns and values of GridControl WPF control at runtime from my custom List of fields or dictionary 我只需要在运行时从我的自定义字段列表或字典中创建GridControl WPF控件的列和值

List<Field> Fields {get;set;}

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

In test stage 在测试阶段

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);

Please How to archive this : 请如何存档:

  • I don't want to use DataSet with GridControl 我不想将DataSet与GridControl一起使用
  • I don't want to useDatatable with GridControl 我不想将Datatable与GridControl一起使用
  • I don't want to use ADO.NET with GridControl 我不想将ADO.NET与GridControl一起使用

i Just want to fill GridControl from Collection of fields. 我只想从字段集合中填充GridControl。 Any suggestion like LINQ, dynamic binding will be appreaciated. 像LINQ,动态绑定之类的任何建议都会得到体现。

Please Have a look following code; 请看下面的代码;

Note: Due to limited time, I have converted final object (ExpendoObject) into anonymous type by using Newtonsoft json (as expendo object cannot bind to grid). 注意:由于时间有限,我已使用Newtonsoft json将最终对象(ExpendoObject)转换为匿名类型(因为expendo对象无法绑定到网格)。 You can improve the code. 您可以改进代码。

Moreover I have added Additional property "RowNumber" in Field class, which will instruct program to map column in specific row. 此外,我在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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM