【问题标题】:Telerik Grid dynamic column names and values are not displaying from the DatatableTelerik Grid 动态列名称和值未从数据表中显示
【发布时间】:2012-10-30 06:15:39
【问题描述】:

我的代码:

  @(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 名称和值。请帮忙。谢谢。

【问题讨论】:

    标签: c# c#-4.0 razor telerik telerik-grid


    【解决方案1】:
    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() 中的列列表。

    【讨论】:

    • 如何初始化。列是动态的,我们从数据表中获取。请帮忙。谢谢。
    • 您需要知道您的查询 reportService.ExecuteQuery(query) 将返回什么(哪些列)。顺便说一句,你能描述一下你想在你的网格中显示什么吗?这个代码行'return View(new GridModel(sample(dt)))'应该做什么?
    • 'return View(new GridModel(sample(dt)))' 返回数据表值。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2023-03-07
    相关资源
    最近更新 更多