Kendo Grid绑定到从List <t>转换的数据表,其中包含对象数组

时间:2016-07-29 12:39:59

标签: asp.net-mvc razor datatable kendo-grid

我已根据存储过程结果转换了List个对象(List<Result&gt;)。对象Result包含许多基本类型属性,array个对象(类型Lot),其大小和其他几个对象(Comment,{ReviewComment {1}}):

    public int? ID { get; set; }
    public DateTime ResultDateTime { get; set; }
    public string EnteredByInitials { get; set; }
    public int ControlSetLotCount { get; set; }
    public Lot[] LotResults { get; set; }
    public Comment StandardComment { get; set; }
    public ReviewComment ReviewComment { get; set; }

然后我将此列表转换为DataTable,以便成功绑定到Kendo Grid控件。

我用来完成转换的功能如下:

private static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    if (Props[i].PropertyType == typeof(int) || Props[i].PropertyType == typeof(string))
                    {
                        //inserting property values to datatable rows
                        values[i] = Props[i].GetValue(item, null);
                    }
                }
                dataTable.Rows.Add(values);
            }

            PropertyInfo[] propList = items.GetType().GetProperties(); //This will get all property with property name
            foreach (PropertyInfo pInfo in propList)
            {
                if (pInfo.PropertyType == typeof(int) || pInfo.PropertyType == typeof(string))
                {
                    //Just get the value and insert to your table
                    object propValue = pInfo.GetValue(items, null); //Notice this is not fit for array type
                }
                else
                {
                    //This is embeded object
                    string thisPropName = pInfo.Name; //Get the property name. Here should be UserType
                    object propValue = pInfo.GetValue(items, null); //Then you can use this object to get its inside property name and value with same method above.
                }
            }            
            return dataTable;
        }

绑定此DataTable会产生以下结果:

enter image description here

正如您所看到的,这不是我想要的最终结果。我想要显示&#39; n&#39;基于Lot数组大小的多标题列数,其中包含各自的属性以及Comment个对象的各个属性。有没有办法可以操纵DataTable视图中的razor来实现这一目标?如果失败了,有没有办法以这样的方式设置DataTable这些对象属性?

我目前正在设置Kendo Grid控件,如下所示(并且已经尝试循环遍历LotResults列的位置但没有成功):

 @(Html.Kendo().Grid<dynamic>()
                .Name("resultGrid")
                .Columns(columns =>
                {
                    var lotResults = Model.Columns[4];
                    foreach (System.Data.DataColumn column in Model.Columns)
                    {
                        if (column.ColumnName == lotResults.ColumnName)
                        {

                        }
                        else
                            columns.Bound(column.ColumnName);

                    }
                    columns.Command(cmd => cmd.Destroy());                    
                })
                .Editable(editable => editable
                    .Mode(GridEditMode.InLine)
                    .DisplayDeleteConfirmation(true))
                .Pageable()
                .Navigatable()
                .Sortable()
                .Groupable()
                .Scrollable()
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .Batch(true)
                    .PageSize(20)
                    .ServerOperation(false)
                    .Events(events => events.Error("error_handler"))
                    .Read("ResultsAsync_Read", "ResultEntry")
                    .Destroy("ResultsAsync_Destroy", "ResultEntry")
                    .Model(model =>
                    {
                        var id = Model.Columns[0].ColumnName;
                        model.Id(id);
                        foreach (System.Data.DataColumn column in Model.Columns)
                        {
                            var field = model.Field(column.ColumnName, column.DataType);
                            if (column.ColumnName == id)
                            {
                                field.Editable(false);
                            }

                        }
                    })
                 )
        )

0 个答案:

没有答案