大家都知道DataGrid控件帶有內置的事件來對記錄進行排序,用戶可單擊列標題,就會按照相應的列進行排序,使用
非常方便和簡單化.
但是它也有很大的不足,就是由datagrid代理的分頁功能,只能單向排序,而且換頁後,排序信息就會丟失.所以我
們需要改進一下分頁的事件函數,利用狀態包ViewState變量,達到能穩固的雙向排序。:)
(1)首先在頁面load時.要讀取數據源裡的數據,並將其送入到DataGrid
private void page_load(Object obj,EventArgs e)
{
if(!Page.IsPostBack)
{
DataLoad();
}
}
private void DataLoad()
{
string connstr=ConfigurationSettings.AppSettings["ConnectionString"];
OleDbConnection conn=new OleDbConnection(connstr);
DataSet ds=new DataSet();
string sql;
if(ViewState["sorting"]==null)
{
sql="select * from userinfo";
Response.Write(sql);
}
else
{
sql="select * from userinfo order by "+ViewState["sorting"].ToString()+"
"+ViewState["sortdirection"].ToString();
Response.Write(sql);
}
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds);
DataView dv=new DataView(ds.Tables[0]);
grid1.DataSource=dv;
grid1.DataBind();
}
(2)然後在datagrid控件的屬性設定中加入如下選項;
...
allowpaging="true"
pagesize=13
pagerstyle-mode=numericpages
onpageindexchanged="changepage"
...
其中allowpaging="true",表示將啟用分頁功能,每頁的大小,我們設定小一些(pagesize=13),好讓它多分頁.
pagerstyle-mode=numericpages是將分頁模式設為數字,最後指定了事件的處理程序為changepage方法.
private void changepage(Object obj,DataGridPageChangedEventArgs e)
{
grid1.CurrentPageIndex=e.NewPageIndex;//當前頁等於新的頁面
DataLoad();
}
(3)最後是排序的事件函數:
private void grid_sort(Object obj,DataGridSortCommandEventArgs e)
{
ViewState.Add("sorting",e.SortExpression);
if (ViewState["sortdirection"]==null)
ViewState.Add("sortdirection","ASC");
else
{
if(ViewState["sortdirection"].ToString()=="ASC")
ViewState["sortdirection"]="DESC";
else
ViewState["sortdirection"]="ASC";
}
DataLoad();
}
整個完整程序為:
<%@page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
<script language="c#" runat="server">
private void page_load(Object obj,EventArgs e)
{
if(!Page.IsPostBack)
{
DataLoad();
}
}
private void DataLoad()
{
string connstr=ConfigurationSettings.AppSettings["ConnectionString"];
OleDbConnection conn=new OleDbConnection(connstr);
DataSet ds=new DataSet();
string sql;
if(ViewState["sorting"]==null)
{
sql="select * from userinfo";
Response.Write(sql);
}
else
{
sql="select * from userinfo order by "+ViewState["sorting"].ToString()+"
"+ViewState["sortdirection"].ToString();
Response.Write(sql);
}
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds);
DataView dv=new DataView(ds.Tables[0]);
grid1.DataSource=dv;
grid1.DataBind();
}
private void changepage(Object obj,DataGridPageChangedEventArgs e)
{
grid1.CurrentPageIndex=e.NewPageIndex;//當前頁等於新的頁面
DataLoad();
}
private void grid_sort(Object obj,DataGridSortCommandEventArgs e)
{
ViewState.Add("sorting",e.SortExpression);
if (ViewState["sortdirection"]==null)
ViewState.Add("sortdirection","ASC");
else
{
if(ViewState["sortdirection"].ToString()=="ASC")
ViewState["sortdirection"]="DESC";
else
ViewState["sortdirection"]="ASC";
}
DataLoad();
}
</script>
<form runat="server">
<asp:datagrid id="grid1" runat="server"
alternatingitemstyle-backcolor="#feeeee"
ItemStyle-BackColor="#CCCCCC"
AutoGenerateColumns="false"
headerstyle-backcolor="lightyellow"
font-size="10pt"
bordercolor="#999999"
allowpaging="true"
pagesize=13
pagerstyle-mode=numericpages
onpageindexchanged="changepage"
AllowSorting="True"
onsortcommand="grid_sort"
>
<columns>