To continue sorting after turning the page, achieve this effect:
For example, there are 15 records in total, and 10 records are displayed on each page
. When sorting: the first 10 records are sorted on the first page, and the next five records are sorted separately when the second page is turned.
Pay attention to the following points:
1. If there is a lot of data, it is best not to put the data set in the cache (viewstate), which will affect performance
. 2. The ViewState stores the last e.expression and whether the e.expression is in ascending or descending order.
Examples are as follows:
1. The existing sorting event is written like this. This is used when clicking on the sorting title above:
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 order, 1 ascending order 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 myself and is called in the page turning event.
private void ChangePageDataBind()
{
DataView dv = get data code;
string strSort = "";
string strOrder = ""; // Sorting method. 0, descending order, 1 ascending order 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 called directly as long as the name of the field to be sorted is modified.
1. The method is very simple and practical, so I won’t go into it here.
2. The method is used like this:
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}
}