我的代码:
@(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网格显示空,没有网格列名称和值。请帮忙。感谢。
答案 0 :(得分: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()
中的列列表。