数据绑定对象的集合或列表排序时,一个gridview [英] Sorting a gridview when databinding a collection or list of objects
问题描述
我有一个GridView通过以下方式设置:
- 绑定到
列表< T>
在code-背后(我用我自己的自定义BOL) - HTML页面上没有任何数据源对象
- 在我选择(即
SORTEX pression
s的所有设置是否正确) 每一列排序
不过,我收到以下错误消息:
GridView控件myGridView解雇事件排序这是不处理的。
块引用>什么是我得到的最好方法我
列表< T>
允许排序我怀疑,这将有指定的
OnSorting
属性的功能,即要做到:OnSorting =MySortingMethod
解决方案感谢您对排序你的答案。我转过身来,LINQ帮助排序动态。由于电网知道是否排序ASC或DESC,哪些领域,我用了LINQ防爆pression。防爆pression进行排序,然后我只需绑定了这些结果,以我的GridView控件。
我怀疑jQuery的方法会更快,也不会需要一个完整的回发。
使用System.Linq.Ex pressions;公共SortDirection GridViewSortDirection
{
得到
{
如果(的ViewState [sortDirection] == NULL)
的ViewState [sortDirection] = SortDirection.Ascending; 回报(SortDirection)的ViewState [sortDirection];
}
集合{的ViewState [sortDirection] =值; }
}保护无效gridView_Sorting(对象发件人,GridViewSortEventArgs E)
{
//重新运行查询,使用LINQ基础上,ARG的对象进行排序。
//执行使用给定的约束搜索
//你可能有这样的保存在会话,而不是再次查询数据存储
清单< T> myGridResults = PerfomSearch();
如果(myGridResults!= NULL)
{
VAR参数=前pression.Parameter(typeof运算(T),e.SortEx pression);
VAR SORTEX pression =前pression.Lambda<&Func键LT; T,对象>>(防爆pression.Convert(前pression.Property(参数,e.SortEx pression)的typeof(对象)),参数);
如果(GridViewSortDirection == SortDirection.Ascending)
{
myGridView.DataSource = myGridResults.AsQueryable< T>()排序依据(SORTEX pression);
GridViewSortDirection = SortDirection.Descending;
}
其他
{
myGridView.DataSource = myGridResults.AsQueryable< T>()OrderByDescending(SORTEX pression);
GridViewSortDirection = SortDirection.Ascending;
};
myGridView.DataBind();
}
}I have a GridView set up in the following way:
- bound to a
List<T>
in code-behind (I am using my own custom BOL)- no DataSource Object on the HTML page
- sortable on each column that I choose (the
SortExpression
s are all set correctly)However, I am getting the following error message:
The GridView 'myGridView' fired event Sorting which wasn't handled.
What is the best way for me to get my
List<T>
to allow sorting?I am suspecting that it will have to do with specifying a function for the
OnSorting
attribute, i.e.:OnSorting = "MySortingMethod"
解决方案Thank you for your answers on the sorting. I turned to LINQ to help sort dynamically. Since the grid knows whether to sort ASC or DESC, and which field, I used a LINQ Expression. The Expression performed the sorting, and then I simply bound those results to my gridview.
I suspect the jQuery method would be faster, and wouldn't require a full postback.
using System.Linq.Expressions; public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } protected void gridView_Sorting(object sender, GridViewSortEventArgs e) { //re-run the query, use linq to sort the objects based on the arg. //perform a search using the constraints given //you could have this saved in Session, rather than requerying your datastore List<T> myGridResults = PerfomSearch(); if (myGridResults != null) { var param = Expression.Parameter(typeof(T), e.SortExpression); var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param); if (GridViewSortDirection == SortDirection.Ascending) { myGridView.DataSource = myGridResults.AsQueryable<T>().OrderBy(sortExpression); GridViewSortDirection = SortDirection.Descending; } else { myGridView.DataSource = myGridResults.AsQueryable<T>().OrderByDescending(sortExpression); GridViewSortDirection = SortDirection.Ascending; }; myGridView.DataBind(); } }
这篇关于数据绑定对象的集合或列表排序时,一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!