如何在单击ASP.NET GridView中的列时更改默认排序方向?
我希望它能够在单击新列时按默认情况下以DESC顺序排序,而不是ASC。
protected void OnGridViewSorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = RoomsGrid.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " +ConvertSortDirectionToSql(e.SortDirection);
RoomsGrid.DataSource = dataView;
RoomsGrid.DataBind();
}
}
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending: newSortDirection = "ASC"; break;
case SortDirection.Descending: newSortDirection = "DESC"; break;
}
return newSortDirection;
}
以下是它现在的行为示例: http://www.venuefinder.com/venues/national_motorcycle_museum/V4204/meeting-rooms/
答案 0 :(得分:0)
您无法更改GridView的默认排序方向,但由于您正在使用函数来获取排序方向,因此您可以按照以下链接中的说明手动执行此操作 - http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1418
参考上面的代码,您可以执行以下操作来完成您的要求:
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending: newSortDirection = "DESC"; break;
case SortDirection.Descending: newSortDirection = "ASC"; break;
}
return newSortDirection;
}
希望这有帮助!
答案 1 :(得分:0)
有一篇很好的MSDN文章 - 它提供的示例将ASC作为默认值返回,但您可以轻松地将其更改为DESC。这里:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx#Y576