【发布时间】:2011-02-02 15:55:42
【问题描述】:
如何使用由 ObjectDataSource 绑定的数据在 gridView 中进行排序?
【问题讨论】:
-
很多.....................
标签: asp.net sorting objectdatasource
如何使用由 ObjectDataSource 绑定的数据在 gridView 中进行排序?
【问题讨论】:
标签: asp.net sorting objectdatasource
Here 是之前已经回答过的问题。
对于实际的排序,你会调用
collectionOfObjects.OrderBy(x => x.PropertyToSortOn);
您可以使用开关根据通过 args 传递给方法的内容来更改排序依据。所以它看起来更像这样
switch(propertyName)
{
case "property1":
collectionOfObjects.OrderBy(x => x.PropertyToSortOn);
break;
case "property2":
collectionOfObjects.OrderBy(x => x.OtherPropertyToSortOn);
break;
...
}
希望这会有所帮助! :)
【讨论】:
如果对您来说更容易,为什么不尝试从存储过程或查询中对其进行排序。 也许不是最佳解决方案,但它可能更容易。
编辑
如果您想使用 gridview 控件以编程方式执行此操作,请查看以下代码:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtSortTable = GridView1.DataSource as DataTable;
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
GridView1.DataSource = dvSortedView;
GridView1.DataBind();
}
}
private string getSortDirectionString(SortDirection sortDireciton)
{
string newSortDirection = String.Empty;
if (sortDireciton == SortDirection.Ascending)
{
newSortDirection = "ASC";
}
else
{
newSortDirection = "DESC";
}
return newSortDirection;
}
【讨论】: