Linq 如何保持GridView';什么是排序状态?(上升和下降)
我有一个困扰我很长时间的问题,我非常需要帮助,因为我是.NET的初学者 我正在使用GridView显示查询结果。但是,我没有将其直接注入实体/LINQ数据源,而是手动编写事件代码,如加载、排序和分页。问题在于排序,我无法保持升序/降序状态。我能想到的一个可能的解决方案是缓存状态,但是,我觉得还有另一种方法更简洁。所以,你们能给我提些更合适的建议吗 提前多谢! 下面是我用于排序的代码。显然,Linq 如何保持GridView';什么是排序状态?(上升和下降),linq,sorting,gridview,direction,Linq,Sorting,Gridview,Direction,我有一个困扰我很长时间的问题,我非常需要帮助,因为我是.NET的初学者 我正在使用GridView显示查询结果。但是,我没有将其直接注入实体/LINQ数据源,而是手动编写事件代码,如加载、排序和分页。问题在于排序,我无法保持升序/降序状态。我能想到的一个可能的解决方案是缓存状态,但是,我觉得还有另一种方法更简洁。所以,你们能给我提些更合适的建议吗 提前多谢! 下面是我用于排序的代码。显然,e.SortDirection将始终等于升序,无论我单击列标题多少次 switch (e.SortExpre
e.SortDirection
将始终等于升序
,无论我单击列标题多少次
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;
}
事实上,我刚刚找到了答案。我使用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;
}
受保护的void gvOfflineSub_排序(对象发送器、GridViewSortEventArgs e)
{
IList IList=gvOfflineSub.DataSource作为IList;
DataTable dt=ToDataTable(Ilist);
如果(dt!=null)
{
数据视图数据视图=新数据视图(dt);
dataView.Sort=e.SortExpression+“”+ConvertSortDirectionToSql(e.SortDirection);
gvOfflineSub.DataSource=dataView;
gvOfflineSub.DataBind();
}
}
///
///描述:以下方法用于对Gridview进行排序。
///
///
///
专用字符串转换器SortDirectionToSQL(SortDirection SortDirection)
{
string newSortDirection=string.Empty;
开关(排序方向)
{
案例排序方向。升序:
newSortDirection=“ASC”;
打破
案例排序方向。下行:
newSortDirection=“DESC”;
打破
}
返回新闻方向;
}
受保护的void gvOfflineSub_排序(对象发送器、GridViewSortEventArgs e)
{
IList IList=gvOfflineSub.DataSource作为IList;
DataTable dt=ToDataTable(Ilist);
如果(dt!=null)
{
数据视图数据视图=新数据视图(dt);
dataView.Sort=e.SortExpression+“”+ConvertSortDirectionToSql(e.SortDirection);
gvOfflineSub.DataSource=dataView;
gvOfflineSub.DataBind();
}
}
///
///描述:以下方法用于对Gridview进行排序。
///
///
///
专用字符串转换器SortDirectionToSQL(SortDirection SortDirection)
{
string newSortDirection=string.Empty;
开关(排序方向)
{
案例排序方向。升序:
newSortDirection=“ASC”;
打破
案例排序方向。下行:
newSortDirection=“DESC”;
打破
}
返回新闻方向;
}
如果使用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();
}
如果您使用的是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.net中这样做的吗?你可以从这个线程中获得想法你是在asp.net中这样做的吗?你可以从这个线程中获得想法