给datagrid控件建立稳固的双向排序(asp.net)

类别:.NET开发 点击:0 评论:0 推荐:

给datagrid控件建立稳固的双向排序(asp.net)

大家都知道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>

<asp:templatecolumn headertext="编号" SortExpression="id">
<itemtemplate>
 <span><%#DataBinder.Eval(Container.DataItem,"id")%></span>
</itemtemplate>
</asp:templatecolumn>

<asp:TemplateColumn HeaderText="用户名" SortExpression="username">
  <ItemTemplate>
<span><%#DataBinder.Eval(Container.DataItem,"username")%></span>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:templatecolumn headertext="电话" SortExpression="phone">
<itemtemplate>
 <span><%#DataBinder.Eval(Container.DataItem,"phone")%></span>
</itemtemplate>
</asp:templatecolumn>

<asp:templatecolumn headertext="ip地址" SortExpression="ip_address">
<itemtemplate>
 <span><%#DataBinder.Eval(Container.DataItem,"ip_address")%></span>
</itemtemplate>
</asp:templatecolumn>

</columns>
</asp:datagrid>
</form>

本文地址:http://com.8s8s.com/it/it45813.htm