I'm trying to get a gridview to sort and page manually with no success.
我正在尝试获取一个gridview来排序和页面,但是没有成功。
The problem is that when a user clicks the column they want to sort, it sorts that page, but doesn't sort the datasource (dataview) behind the gridview. So when they progress to a different page, their sort is lost. Pretty much I'm looking for a sort that will actually sort the datasource behind the gridview. Here is what I have so far:
问题是,当用户单击他们想要排序的列时,它会对该页面进行排序,但不会对gridview后面的数据源(dataview)进行排序。因此,当他们进入另一个页面时,他们的排序就会丢失。我正在寻找一种排序,它可以对gridview后面的数据源进行排序。这是我到目前为止所得到的:
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();
}
}
Any help would be appreciated. Thanks.
如有任何帮助,我们将不胜感激。谢谢。
5 个解决方案
#1
52
Save your sorting order in a ViewState.
将排序顺序保存在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();
}
Why you don't want to use existing sorting functionality? You can always customize it.
为什么不希望使用现有的排序功能?您可以随时定制它。
Sorting Data in a GridView Web Server Control at MSDN
在MSDN上的GridView Web服务器控件中对数据进行排序
Here is an example with customization:
这里有一个定制的例子:
http://www.netomatix.com/development/GridViewSorting.aspx
http://www.netomatix.com/development/GridViewSorting.aspx
#2
5
<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>
Code behind:
背后的代码:
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;
}
}
#3
0
I found a much easier way, which allows you to still use the built in sorting/paging of the standard gridview...
我发现了一种更简单的方法,它允许您仍然使用标准gridview的内置排序/分页…
create 2 labels. set them to be visible = false. I called mine lblSort1 and lblSortDirection1
创建2个标签。将它们设置为可见= false。我把我的lblSort1和lblSortDirection1叫做
then code 2 simple events... the page sorting, which writes to the text of the invisible labels, and the page index changing, which uses them...
然后代码2简单事件…页面排序,它写入了看不见的标签的文本,以及页面索引的变化,使用了它们……
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
this is a little sloppier than using global variables, but I've found with asp especially that global vars are, well, unreliable...
这比使用全局变量要容易一些,但我已经发现了asp,特别是全球的vars,嗯,不可靠……
#4
0
More simple way...:
更简单的方式…:
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()
#5
0
Tarkus's answer works well. However, I would suggest replacing VIEWSTATE with SESSION.
Tarkus的回答很有效。但是,我建议用会话替换VIEWSTATE。
The current page's VIEWSTATE only works while the current page posts back to itself and is gone once the user is redirected away to another page. SESSION persists the sort order on more than just the current page's post-back. It persists it across the entire duration of the session. This means that the user can surf around to other pages, and when he comes back to the given page, the sort order he last used still remains. This is usually more convenient.
当前页面的VIEWSTATE仅在当前页面返回到自身时有效,并且在用户被重定向到另一个页面时消失。会话将在不只是当前页面的后返回的情况下保存排序顺序。它在整个会话过程中持续存在。这意味着用户可以浏览其他页面,当他返回到给定页面时,他最后使用的排序规则仍然保留。这通常更方便。
There are other methods, too, such as persisting user profiles.
还有其他方法,比如持久化用户概要文件。
I recommend this article for a very good explanation of ViewState and how it works with a web page's life cycle: https://msdn.microsoft.com/en-us/library/ms972976.aspx
我推荐这篇文章来很好地解释ViewState以及它如何处理web页面的生命周期:https://msdn.microsoft.com/en-us/library/ms972976.aspx
To understand the difference between VIEWSTATE, SESSION and other ways of persisting variables, I recommend this article: https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
为了理解VIEWSTATE、SESSION和其他持久化变量的方法之间的区别,我推荐本文:https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
#1
52
Save your sorting order in a ViewState.
将排序顺序保存在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();
}
Why you don't want to use existing sorting functionality? You can always customize it.
为什么不希望使用现有的排序功能?您可以随时定制它。
Sorting Data in a GridView Web Server Control at MSDN
在MSDN上的GridView Web服务器控件中对数据进行排序
Here is an example with customization:
这里有一个定制的例子:
http://www.netomatix.com/development/GridViewSorting.aspx
http://www.netomatix.com/development/GridViewSorting.aspx
#2
5
<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>
Code behind:
背后的代码:
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;
}
}
#3
0
I found a much easier way, which allows you to still use the built in sorting/paging of the standard gridview...
我发现了一种更简单的方法,它允许您仍然使用标准gridview的内置排序/分页…
create 2 labels. set them to be visible = false. I called mine lblSort1 and lblSortDirection1
创建2个标签。将它们设置为可见= false。我把我的lblSort1和lblSortDirection1叫做
then code 2 simple events... the page sorting, which writes to the text of the invisible labels, and the page index changing, which uses them...
然后代码2简单事件…页面排序,它写入了看不见的标签的文本,以及页面索引的变化,使用了它们……
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
this is a little sloppier than using global variables, but I've found with asp especially that global vars are, well, unreliable...
这比使用全局变量要容易一些,但我已经发现了asp,特别是全球的vars,嗯,不可靠……
#4
0
More simple way...:
更简单的方式…:
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()
#5
0
Tarkus's answer works well. However, I would suggest replacing VIEWSTATE with SESSION.
Tarkus的回答很有效。但是,我建议用会话替换VIEWSTATE。
The current page's VIEWSTATE only works while the current page posts back to itself and is gone once the user is redirected away to another page. SESSION persists the sort order on more than just the current page's post-back. It persists it across the entire duration of the session. This means that the user can surf around to other pages, and when he comes back to the given page, the sort order he last used still remains. This is usually more convenient.
当前页面的VIEWSTATE仅在当前页面返回到自身时有效,并且在用户被重定向到另一个页面时消失。会话将在不只是当前页面的后返回的情况下保存排序顺序。它在整个会话过程中持续存在。这意味着用户可以浏览其他页面,当他返回到给定页面时,他最后使用的排序规则仍然保留。这通常更方便。
There are other methods, too, such as persisting user profiles.
还有其他方法,比如持久化用户概要文件。
I recommend this article for a very good explanation of ViewState and how it works with a web page's life cycle: https://msdn.microsoft.com/en-us/library/ms972976.aspx
我推荐这篇文章来很好地解释ViewState以及它如何处理web页面的生命周期:https://msdn.microsoft.com/en-us/library/ms972976.aspx
To understand the difference between VIEWSTATE, SESSION and other ways of persisting variables, I recommend this article: https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
为了理解VIEWSTATE、SESSION和其他持久化变量的方法之间的区别,我推荐本文:https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx