要想實現翻頁后繼續排序,實現這樣的效果:
比如共 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();
}
2.下面這個方法是自己寫的,翻頁事件中調用。
private void changepagedatabind()
{
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(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();
}
上面兩方法只要修改要排序的字段名,就可以直接調用了.
1方法很簡單使用,這里就不說了.
2方法是這樣用的:
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)
{
//異常
}
}
謝謝閱讀!
新聞熱點
疑難解答
圖片精選