Asp.net| Page | sort
To continue sorting after the paging is done, you will achieve this effect:
For example, a total of 15 records, each page shows 10
Then sort: The first page sorts the top 10 records, and then the second page is sorted after five.
The following points should be noted:
1. If the data is many, it is best not to put the dataset into the cache (ViewState), affect performance
2. ViewState which e.expression to store the last time and to store this e.expression is ascending or descending
Examples are as follows:
1. The existing sort event is written in this way by clicking on the above sort headers:
private void Grdprojtrace_sortcommand (object source, DataGridSortCommandEventArgs e)
{
This.grdProjTrace.CurrentPageIndex = 0;
DataView DV = Get data code;
String strsort = "";
String strorder = "";//Sort 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 written by itself, called in the paging event.
private void Changepagedatabind ()
{
DataView DV = Get data code;
String strsort = "";
String strorder = "";//Sort 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 ();
}
The above two methods can be invoked directly if you modify the name of the field to be sorted.
1, the method is very simple and practical, here will not say.
2, the method is this use:
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)
{
Abnormal
}
}