1

我的代码:

  @(Html.Telerik().Grid<System.Data.DataRow>()
            .Name("reportsGrid")
                    .ClientEvents(events =>
                    {
                        events.OnDataBinding("onDataBinding");
                    })
                    .DataBinding(dataBinding => dataBinding.Ajax().Select("ReportsGrid", "Reports", new { _query }).Enabled(true))
            .Columns(columns =>
            {
                if (Model != null)
                {
                    foreach (DataColumn column in Model.Columns)
                    {
                        columns.Bound(column.DataType, column.ColumnName);
                    }
                }
            })

我的控制器代码:

[GridAction]
        [HttpPost]
        public ActionResult ReportsGrid(string query)
        {
            var reportService = new ReportService();
            System.Data.DataTable dt = reportService.ExecuteQuery(query);
            if (dt != null)
                return View(new GridModel(sample(dt)));
                //return View(new GridModel { Data = sample(dt) });
            else
                return View(new GridModel { Data = null });
        }


        private IEnumerable<Dictionary<string, Object>> sample(System.Data.DataTable dt)
        {
            List<Dictionary<string, Object>> rows = new List<Dictionary<string, Object>>();
            Dictionary<string, Object> row = new Dictionary<string, Object>();
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, Object>();
                foreach (System.Data.DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }

            return rows.ToList();
        }

        public ActionResult ReportsGrid()
        {
            var reportService = new ReportService();
            //System.Data.DataTable dt = reportService.ExecuteQuery(query);
            return PartialView("ReportsGrid", new System.Data.DataTable());
        }

我的 Telerik Grid 显示为空,没有 Grid Column 名称和值。请帮忙。谢谢。

4

1 回答 1

0
public ActionResult ReportsGrid()
{
    var reportService = new ReportService();
    //System.Data.DataTable dt = reportService.ExecuteQuery(query);
    return PartialView("ReportsGrid", new System.Data.DataTable());
}

此控制器方法返回没有任何列的空数据表。所以这段代码:

foreach (DataColumn column in Model.Columns)
{
    columns.Bound(column.DataType, column.ColumnName);
}

不会迭代(Model.Columns 为空可枚举)。要解决您的问题,您需要使用列初始化数据表。

如果您有动态数据表,您应该ActionResult ReportsGrid()通过另一个查询获取方法中的列列表。

于 2012-10-30T06:22:25.043 回答