如何保持GridView的排序状态? (升序和降序)
问题描述:
我有一个问题,一直在困扰我很长一段时间,我急需帮助,因为我是一个.NET初学者。如何保持GridView的排序状态? (升序和降序)
我正在使用GridView来显示查询结果。然而,我不是直接将其注入到实体/ LINQ数据源,而是手动编写诸如加载,排序和分页之类的事件。问题在于排序,我无法保持上升/下降状态。我可以想到的一个可能的解决方案是通过缓存状态,但是,我觉得还有另一种更整洁的方式。因此,你们可以向我暗示其他更适合的想法吗?
非常感谢! 下面是我用于排序的代码。显然,e.SortDirection
将永远等于ascending
无论我点击了列的标题多少次。
switch (e.SortExpression)
{
case "Album":
if (e.SortDirection == SortDirection.Ascending)
_orderedResult = from doc in _result
orderby doc.DocumentAlbum.Name ascending
select doc;
else
_orderedResult = from doc in _result
orderby doc.DocumentAlbum.Name descending
select doc;
break;
case "Category":
if (e.SortDirection == SortDirection.Ascending)
_orderedResult = from doc in _result
orderby doc.DocumentCategory.Name ascending
select doc;
else
_orderedResult = from doc in _result
orderby doc.DocumentCategory.Name descending
select doc;
break;
case "Title":
if (e.SortDirection == SortDirection.Ascending)
_orderedResult = from doc in _result
orderby doc.Title ascending
select doc;
else
_orderedResult = from doc in _result
orderby doc.Title descending
select doc;
break;
case "Description":
if (e.SortDirection == SortDirection.Ascending)
_orderedResult = from doc in _result
orderby doc.Description ascending
select doc;
else
_orderedResult = from doc in _result
orderby doc.Description descending
select doc;
break;
case "DateCreated":
if (e.SortDirection == SortDirection.Ascending)
_orderedResult = from doc in _result
orderby doc.DateCreated ascending
select doc;
else
_orderedResult = from doc in _result
orderby doc.DateCreated descending
select doc;
break;
case "DateUpdated":
if (e.SortDirection == SortDirection.Ascending)
_orderedResult = from doc in _result
orderby doc.DateUpdated ascending
select doc;
else
_orderedResult = from doc in _result
orderby doc.DateUpdated descending
select doc;
break;
}
答
其实我只是找到了答案。我使用ViewState函数来跟踪状态。 这是我使用的功能:
private SortDirection GetSortDirection(string column)
{
// By default, set the sort direction to ascending
SortDirection _sortDirection = SortDirection.Ascending;
// Retrieve the last column that was sorted
string _sortExpression = ViewState["SortExpression"] as string;
if (_sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (_sortExpression == column)
{
string _lastDirection = ViewState["SortDirection"] as string;
if ((_lastDirection != null) && (_lastDirection == "ASC"))
{
_sortDirection = SortDirection.Descending;
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = _sortDirection.ToString();
ViewState["SortExpression"] = column;
return _sortDirection;
}
答
protected void gvOfflineSub_Sorting(object sender, GridViewSortEventArgs e)
{
IList<SellerDetails> Ilist = gvOfflineSub.DataSource as IList<SellerDetails>;
DataTable dt = ToDataTable<SellerDetails>(Ilist);
if (dt != null)
{
DataView dataView = new DataView(dt);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
gvOfflineSub.DataSource = dataView;
gvOfflineSub.DataBind();
}
}
/// <summary>
/// Description:Following Method is for Sorting Gridview.
/// </summary>
/// <param name="sortDirection"></param>
/// <returns></returns>
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;
}
答
如果您正在使用LINQ那么它是非常简单的排序 只是绑定电网这样
var data = (from d in edc.TableName
select new
{
d.Address,
d.InsertedDate,
d.ID
}).OrderByDescending(d => d.ID);
if (data != null)
{
grdList.DataSource = data;
grdList.DataBind();
}
你在ASP这样做。净? – 2011-05-21 17:53:13
你可以从这个线索得到想法http://*.com/questions/5947780/how-to-convert-a-gridview-to-datatable-and-sort-the-datatable/5947912#5947912 – 2011-05-21 18:06:07