本文介绍了从填充DataTable的MVC的WebGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用它内置了背后的code再发枚举使用AsEnumerable()扩展方法一个DataTable来填充MVC的WebGrid。
然而,当我称之为GetHtml方法,输出是不是我所期望的,它由两列HasErrors和RowError,没有我所定义的列。
我缺少的东西吗?
DataTable的表=新的DataTable();
table.Columns.Add(I /直径); 的foreach(在Variations.Where(项目=&GT VAR项目;!table.Columns.Contains(item.CrossSectionalDiameter)))
{
table.Columns.Add(item.CrossSectionalDiameter);
} 的foreach(在变化VAR项)
{
变种R = table.Rows.Add();
- [R [I /直径] = item.InternalDiameter;
- [R [item.CrossSectionalDiameter] = item.Price;
} 返回table.AsEnumerable();
解决方案
我有类似的问题。最后做一些工作,各地可根据您的评论。
VAR的结果=新的List<动态>();
的foreach(在table.Rows的DataRow行)
{
VAR OBJ =(IDictionary的<字符串对象>)新ExpandoObject();
的foreach(在table.Columns的DataColumn COL)
{
obj.Add(col.ColumnName,行[col.ColumnName]);
}
result.Add(OBJ);
}
VAR电网=新的WebGrid(结果)
I am trying to populate a MVC Webgrid using a DataTable which is built up in the code behind and then made enumerable using the AsEnumerable() extension method.
However, when I call the GetHtml method, the output is not what I expect, it consists of two columns HasErrors and RowError and none of the columns I have defined.
Am I missing something?
DataTable table = new DataTable();
table.Columns.Add("I/Dia");
foreach (var item in Variations.Where(item => !table.Columns.Contains(item.CrossSectionalDiameter)))
{
table.Columns.Add(item.CrossSectionalDiameter);
}
foreach (var item in Variations)
{
var r = table.Rows.Add();
r["I/Dia"] = item.InternalDiameter;
r[item.CrossSectionalDiameter] = item.Price;
}
return table.AsEnumerable();
解决方案
I have the similar question. Finally make some work around according to your comment.
var result = new List<dynamic>();
foreach (DataRow row in table.Rows)
{
var obj = (IDictionary<string, object>)new ExpandoObject();
foreach (DataColumn col in table.Columns)
{
obj.Add(col.ColumnName, row[col.ColumnName]);
}
result.Add(obj);
}
var grid = new WebGrid(result)
这篇关于从填充DataTable的MVC的WebGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!