要想實現翻頁後繼續排序,實現這樣的效果:
比如共 15筆記錄,每頁顯示10條
則排序時:第一頁將前10條記錄排序,翻第二頁時後五條再單獨排序.
要注意以下幾點:
1.如果數據很多,最好不要把數據集放到緩存中(viewstate),影響性能
2. ViewState中存放上次是哪個e.expression並且存放此e.expression是升 序還是降序
示例如下:
1.現有的排序事件是這樣寫的,這個是點擊上面排序標題時用:
private void grdProjTrace_SortCommand(object source, DataGridSortCommandEventArgs e)
{
this.grdProjTrace.CurrentPageIndex = 0;
DataView dv = 得到數據代碼;
string strSort = "";
string strOrder ="";//排序方式。0,降序,1升序
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();
}