To continue sorting after turning pages, this effect is achieved:
For example, a total of 15 records are displayed, and 10 records are displayed on each page.
Sort the first 10 records on the first page, and then sort the last five records on the second page.
Note the following:
1. If there is a lot of data, it is best not to put the dataset into the cache (viewstate), affecting performance
2. The ViewState stores the last e. expression and whether the e. expression is in ascending or descending order.
Example:
1. The existing sorting event is written in this way. This is used when you click the above sorting title:
Private void grdProjTrace_SortCommand (object source, DataGridSortCommandEventArgs e)
{
This. grdProjTrace. CurrentPageIndex = 0;
DataView dv = get data code;
String strSort = "";
String strOrder = ""; // sorting method. 0, descending, 1 ascending
If (ViewState ["SortExpresstion"]! = Null)
{
StrSort = ViewState ["SortExpresstion"]. ToString ();
StrSort = strSort. Substring (0, strSort. Length-1 );
StrOrder = ViewState ["SortExpresstion"]. ToString ();
StrOrder = strOrder. Substring (strOrder. Length-1 );
}
If (e. SortExpression = "CustomerName ")
{
If (strSort! = "CustomerName ")
{
This. ViewState ["SortExpresstion"] = ustomerName0 ";
Dv. Sort = "CustomerName DESC ";
}
Else
{
If (strOrder = "0 ")
{
This. ViewState ["SortExpresstion"] = "CustomerName1 ";
Dv. Sort = "CustomerName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "CustomerName0 ";
Dv. Sort = "CustomerName DESC ";
}
}
}
If (e. SortExpression = "fullName ")
{
If (strSort! = "FullName ")
{
This. ViewState ["SortExpresstion"] = "fullName0 ";
Dv. Sort = "fullName DESC ";
}
Else
{
If (strOrder = "0 ")
{
This. ViewState ["SortExpresstion"] = "fullName1 ";
Dv. Sort = "fullName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "fullName0 ";
Dv. Sort = "fullName DESC ";
}
}
}
This. grdProjTrace. DataSource = dv;
This. grdProjTrace. DataBind ();
}
2. The following method is self-written and called in the page flip event.
Private void ChangePageDataBind ()
{
DataView dv = get data code;
String strSort = "";
String strOrder = ""; // sorting method. 0, descending, 1 ascending
If (ViewState ["SortExpresstion"]! = Null)
{
StrSort = ViewState ["SortExpresstion"]. ToString ();
StrSort = strSort. Substring (0, strSort. Length-1 );
StrOrder = ViewState ["SortExpresstion"]. ToString ();
StrOrder = strOrder. Substring (strOrder. Length-1 );
}
If (this. ViewState ["SortExpresstion"]! = Null)
{
If (strSort = "CustomerName ")
{
If (strOrder = "1 ")
{
This. ViewState ["SortExpresstion"] = "CustomerName1 ";
Dv. Sort = "CustomerName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "CustomerName0 ";
Dv. Sort = "CustomerName DESC ";
}
}
}
If (this. ViewState ["SortExpresstion"]! = Null)
{
If (strSort = "fullName ")
{
If (strOrder = "1 ")
{
This. ViewState ["SortExpresstion"] = "fullName1 ";
Dv. Sort = "fullName ASC ";
}
Else
{
This. ViewState ["SortExpresstion"] = "fullName0 ";
Dv. Sort = "fullName DESC ";
}
}
}
This. grdProjTrace. DataSource = dv;
This. grdProjTrace. DataBind ();
}
You can directly call the above two methods by modifying the field names to be sorted.
1. This method is easy to use.
The 2 method is used as follows:
Private void grdProjTrace_PageIndexChanged (object source, DataGridPageChangedEventArgs e)
{
Try
{
Try
{
This. grdProjTrace. CurrentPageIndex = e. NewPageIndex;
}
Catch
{
This. grdProjTrace. CurrentPageIndex = 0;
}
This. ChangePageDataBind ();
}
Catch (System. Exception errWS)
{
// Exception
}
}