我有一个问题已经困扰了我很长一段时间,因为我是.NET的初学者,我迫切需要帮助。
我正在使用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;
}
答案 0 :(得分:1)
实际上,我刚刚找到答案。我使用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;
}
答案 1 :(得分:0)
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;
}
答案 2 :(得分:0)
如果您正在使用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();
}