在对GridView进行排序时,我遇到了一个错误。我的数据源是一个名为results的变量,通过一个Linq查询获取得到。
protected void Page_Load(object sender, EventArgs e)
{
dt1 = obj1.Table1data().Tables[0];
dt2 = obj1.Table2data().Tables[0];
dt3 = obj1.Table3data().Tables[0];
var results = (
from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on (int)table1["id"] equals (int)table2["id"]
join table3 in dt3.AsEnumerable() on (int)table1["id"] equals (int)table3["id"]
select new
{
id = (int)table1["id"],
S1= (int)table1["S1"],
P1= (double)table1["P1"],
P2= (int)table2["P2"],
P3= (double)table2["P3"],
P4 = (int)table3["P4"],
P5= (double)table3["P5"],
}).ToList();
Session["ds"] = results;
GridView1.DataSource = results;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataSet dataSet = (DataSet)Session["ds"];
DataTable dataTable = dataSet.Tables[0];
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataSource = dataView;
GridView1.DataBind();
}
}
// here in the GridView1_sorting at DataSet dataSet = (DataSet)Session["ds"], I am getting an error
错误:
Unable to cast object of type System.Collections.Generic.List`1[<>f__AnonymousType5`8[System.Int32,System.String,System.Int32,System.Double,System.Int32,System.Double,System.Int32,System.Double]]' to type 'System.Data.DataSet'
2) 另外一个问题,变量 results 的数据类型是什么。谢谢。Sun
var list = (List)Session["ds"];....
.....
}这些属性只是我为这篇文章随机命名的。
- user575219OrderBy
方法进行构建。例如,var query = list.AsEnumerable(); if (e.SortExpression == "ID") { query = query.OrderBy(item => item.ID); }
搜索带有对象列表的网格视图以获取更多想法。 - Anthony Pegram