大家都知道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();
}
整个完整程序为:
<Mailto:%@page" rel="nofollow">%@page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
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();
}
RM 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"
>
IP地址" SortExpression="ip_address">
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-993823/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10752043/viewspace-993823/