【发布时间】:2014-10-21 12:02:29
【问题描述】:
即使看了几个例子,我似乎也无法理解这一点。
我有这段代码,它很高兴按升序重新排序我的网格视图:
// gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are
// clicked. You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side
protected void gridViewSorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GVInactive.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
string SQL = "[" + e.SortExpression + "] " + ConvertSortDirectionToSql(e.SortDirection);
dataView.Sort = SQL;
GVInactive.DataSource = dataView.ToTable();
GVInactive.DataBind();
}
}
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;
}
但是,我第二次单击标题时,它应该颠倒以前的排序顺序。它永远不会。每次我单击列的标题时,它都会点击case SortDirection.Ascending: 行并设置newSortDirection = "DESC"。数据按降序排序,但是当我再次单击标题时,它将 SortDirection 解析为升序。
有什么想法吗?
【问题讨论】:
标签: c# gridview code-behind