即使查看了一些示例,我也無法得到正確的結果。對gridview控件進行排序 - 方向永不改變
我有了這個代碼,它高興地重新排序我的GridView在升序:
// 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解析爲升序。
任何想法?
它看起來像BindGrid()是一個函數。你能否發佈該功能?沒有它我只有一半的答案。另外,我是否需要任何特殊參考才能使其工作? – 2014-08-29 14:29:37
好點。我現在已經使用了BindGrid方法,它顯示了SortCriteria和SortDirection屬性的用法。對不起,疏忽了。 – laughsloudly 2014-08-29 17:02:32