用gridview asp.netsorting和分页

我想获得一个GridViewsorting和手动页面没有成功。

问题是,当用户点击他们想要sorting的列时,它会对该页面进行sorting,但不sortinggridview后面的数据源(dataview)。 所以当他们进入一个不同的页面时,他们的sorting会丢失。 我正在寻找一种sorting,将实际sortingGridView后面的数据源。 这是我到目前为止:

protected void GridView_OnSort(object sender, GridViewSortEventArgs e) { String sortExpression = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { DataView myDataView = new DataView(mybll.GetItemsOrdered()); myDataView.Sort = sortExpression + " DESC"; GridView.DataSource = myDataView; GridView.DataBind(); } else { DataView myDataView = new DataView(mybll.GetItemsOrdered()); myDataView.Sort = sortExpression + " ASC"; GridView.DataSource = myDataView; GridView.DataBind(); } } 

任何帮助,将不胜感激。 谢谢。

将您的sorting顺序保存在ViewState中。

 private const string ASCENDING = " ASC"; private const string DESCENDING = " DESC"; public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection) ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } protected void GridView_Sorting(object sender, GridViewSortEventArgs e) { string sortExpression = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { GridViewSortDirection = SortDirection.Descending; SortGridView(sortExpression, DESCENDING); } else { GridViewSortDirection = SortDirection.Ascending; SortGridView(sortExpression, ASCENDING); } } private void SortGridView(string sortExpression,string direction) { // You can cache the DataTable for improving performance DataTable dt = GetData().Tables[0]; DataView dv = new DataView(dt); dv.Sort = sortExpression + direction; GridView1.DataSource = dv; GridView1.DataBind(); } 

为什么你不想使用现有的分类function? 你可以随时定制它。

在MSDN 的GridView Web服务器控件中sorting数据

这是一个自定义的例子:

http://www.netomatix.com/development/GridViewSorting.aspx

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="True" onsorting="GridView1_Sorting" EnableViewState="true"> <Columns> <asp:BoundField DataField="bookid" HeaderText="BOOK ID"SortExpression="bookid" /> <asp:BoundField DataField="bookname" HeaderText="BOOK NAME" /> <asp:BoundField DataField="writer" HeaderText="WRITER" /> <asp:BoundField DataField="totalbook" HeaderText="TOTALBOOK" SortExpression="totalbook" /> <asp:BoundField DataField="availablebook" HeaderText="AVAILABLE BOOK" /> </Columns> </asp:GridView> 

代码后面:

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string query = "SELECT * FROM book"; DataTable DT = new DataTable(); SqlDataAdapter DA = new SqlDataAdapter(query, sqlCon); DA.Fill(DT); GridView1.DataSource = DT; GridView1.DataBind(); } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string query = "SELECT * FROM book"; DataTable DT = new DataTable(); SqlDataAdapter DA = new SqlDataAdapter(query, sqlCon); DA.Fill(DT); GridView1.DataSource = DT; GridView1.DataBind(); if (DT != null) { DataView dataView = new DataView(DT); dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); GridView1.DataSource = dataView; GridView1.DataBind(); } } private string GridViewSortDirection { get { return ViewState["SortDirection"] as string ?? "DESC"; } set { ViewState["SortDirection"] = value; } } private string ConvertSortDirectionToSql(SortDirection sortDirection) { switch (GridViewSortDirection) { case "ASC": GridViewSortDirection = "DESC"; break; case "DESC": GridViewSortDirection = "ASC"; break; } return GridViewSortDirection; } } 

我发现一个更简单的方法,它允许您仍然使用内置的sorting/分页的标准gridview …

创build2个标签。 将它们设置为可见= false。 我叫我的lblSort1和lblSortDirection1

然后代码2简单的事件…页面sorting,写入不可见标签的文本,页面索引更改,使用它们…

 Private Sub gridview_Sorting(sender As Object, e As GridViewSortEventArgs) Handles gridview.Sorting lblSort1.Text = e.SortExpression lblSortDirection1.Text = e.SortDirection End Sub Private Sub gridview_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridview.PageIndexChanging gridview.Sort(lblSort1.Text, CInt(lblSortDirection1.Text)) End Sub 

这是比使用全局variables稍微sloppier,但我发现与asp特别是,全球variables,是不可靠的… …

更简单的方法…:

  Dim dt As DataTable = DirectCast(GridView1.DataSource, DataTable) Dim dv As New DataView(dt) If GridView1.Attributes("dir") = SortDirection.Ascending Then dv.Sort = e.SortExpression & " DESC" GridView1.Attributes("dir") = SortDirection.Descending Else GridView1.Attributes("dir") = SortDirection.Ascending dv.Sort = e.SortExpression & " ASC" End If GridView1.DataSource = dv GridView1.DataBind() 

Tarkus的回答很好。 不过,我build议用SESSIONreplaceVIEWSTATE。

当前页面的VIEWSTATE只在当前页面发回自己时才起作用,一旦用户被redirect到另一页面,该页面就会消失。 SESSION不仅保留了当前页面的回传,还保留了sorting顺序。 它在整个会议期间一直持续下去。 这意味着用户可以浏览其他页面,当他回到给定的页面时,他最后使用的sorting顺序仍然存在。 这通常更方便。

还有其他一些方法,例如持久化用户configuration文件。

我推荐这篇文章对ViewState的一个很好的解释,以及它如何处理网页的生命周期: https : //msdn.microsoft.com/en-us/library/ms972976.aspx

要理解VIEWSTATE,SESSION和其他持久variables的不同之处,我推荐这篇文章: https ://msdn.microsoft.com/en-us/library/75x4ha6s.aspx