我有一个网格视图,当用户单击标题时,我需要对它的元素进行排序。
它的数据源是一个列表对象。
aspx是这样定义的:
<asp:GridView ID="grdHeader" AllowSorting="true" AllowPaging="false"
AutoGenerateColumns="false" Width="780" runat="server" OnSorting="grdHeader_OnSorting" EnableViewState="true">
<Columns>
<asp:BoundField DataField="Entitycode" HeaderText="Entity" SortExpression="Entitycode" />
<asp:BoundField DataField="Statusname" HeaderText="Status" SortExpression="Statusname" />
<asp:BoundField DataField="Username" HeaderText="User" SortExpression="Username" />
</Columns>
</asp:GridView>后台代码是这样定义的:
第一次加载:
protected void btnSearch_Click(object sender, EventArgs e)
{
List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
this.grdHeader.DataSource = items;
this.grdHeader.DataBind();
}当用户点击标题时:
protected void grdHeader_OnSorting(object sender, GridViewSortEventArgs e)
{
List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
items.Sort(new Helpers.GenericComparer<V_ReportPeriodStatusEntity>(e.SortExpression, e.SortDirection));
grdHeader.DataSource = items;
grdHeader.DataBind();
}我的问题是e.SortDirection总是设置为升序。
我有一个类似的代码的网页,它的工作很好,e.SortDirection之间的升降交替。
我做错了什么?
发布于 2009-01-06 07:54:24
您可以使用会话变量来存储最新的排序表达式,下次对网格进行排序时,将网格的排序表达式与存储上次排序表达式的会话变量进行比较。如果列相等,则检查前一次排序的方向,并按相反的方向排序。
示例:
DataTable sourceTable = GridAttendence.DataSource as DataTable;
DataView view = new DataView(sourceTable);
string[] sortData = ViewState["sortExpression"].ToString().Trim().Split(' ');
if (e.SortExpression == sortData[0])
{
if (sortData[1] == "ASC")
{
view.Sort = e.SortExpression + " " + "DESC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}发布于 2009-08-19 07:37:57
会话和视图状态的问题是,如果页面上有多个网格视图,则还必须跟踪存储SortColumn和方向的网格视图控件。
Session和Viewstate的替代方法是向Gridview添加2个属性,并以这种方式跟踪列和方向。
下面是一个示例:
private void GridViewSortDirection(GridView g, GridViewSortEventArgs e, out SortDirection d, out string f)
{
f = e.SortExpression;
d = e.SortDirection;
//Check if GridView control has required Attributes
if (g.Attributes["CurrentSortField"] != null && g.Attributes["CurrentSortDir"] != null)
{
if (f == g.Attributes["CurrentSortField"])
{
d = SortDirection.Descending;
if (g.Attributes["CurrentSortDir"] == "ASC")
{
d = SortDirection.Ascending;
}
}
g.Attributes["CurrentSortField"] = f;
g.Attributes["CurrentSortDir"] = (d == SortDirection.Ascending ? "DESC" : "ASC");
}
}发布于 2009-05-01 17:35:14
一个简单的解决方案:
protected SortDirection GetSortDirection(string column)
{
SortDirection nextDir = SortDirection.Ascending; // Default next sort expression behaviour.
if (ViewState["sort"] != null && ViewState["sort"].ToString() == column)
{ // Exists... DESC.
nextDir = SortDirection.Descending;
ViewState["sort"] = null;
}
else
{ // Doesn't exists, set ViewState.
ViewState["sort"] = column;
}
return nextDir;
}非常类似于ViewState上的默认GridView排序和轻量级。
使用:
protected void grdHeader_OnSorting(object sender, GridViewSortEventArgs e)
{
List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
items.Sort(new Helpers.GenericComparer<V_ReportPeriodStatusEntity>(e.SortExpression, GetSortDirection(e.SortExpression));
grdHeader.DataSource = items;
grdHeader.DataBind();
}https://stackoverflow.com/questions/250037
复制相似问题