网上转载技术类

上一篇 / 下一篇  2009-03-03 00:39:55 / 个人分类:技术文章

SortExpression 排序

asp:DataGrid ID="kjkm_dg" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
               
               
                   
                   
                   
               

               
               
               
               
               
               

后台代码:

DAL.DALDB aa = new DAL.DALDB();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (this.kjkm_dg.Attributes["SortExpression"] == null) //这里kjkm_dg为datagrid   ID
            {
                this.kjkm_dg.Attributes["SortExpression"] = "addr"; //这里给datagrid增加一个排序属性,且默认排序表达式为kmdm;
                kjkm_dg.Attributes["SortDirection"] = "ASC"; //这里给datagrid增加一个排序方向属性,且默认为升序排列;
            }
            mikecatbind(); //绑定函数,下面介绍
        }
    }

    protected void mikecatbind()
    {
        string sqlStr = "select distinct b.addr,b.stnm,a.zi from st_rsvr_r as a,st_stinfo_b_other as b where RTRIM(a.stcdt)+convert(varchar(19),a.ymdhm,121) in (select RTRIM(c.stcdt)+convert(varchar(19),max(c.ymdhm),121) from st_rsvr_r c group by stcdt)and a.stcdt = b.stcdt";

        DataView dv = new DataView();

        string SortExpression = kjkm_dg.Attributes["SortExpression"];
        string SortDirection = kjkm_dg.Attributes["SortDirection"];
        dv = aa.ExecuteDataSet(sqlStr).Tables[0].DefaultView; //来自web service的dataset,这里随便一个ds就可以;
        dv.Sort = SortExpression + " " + SortDirection; //指定视图的排序方式;
        kjkm_dg.DataSource = dv; //指定数据源
        kjkm_dg.DataBind(); //数据绑定
    }


    protected void kjkm_dg_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
        string SortExpression = e.SortExpression.ToString(); //获得当前排序表达式
        string SortDirection = "ASC"; //为排序方向变量赋初值
        if (SortExpression == kjkm_dg.Attributes["SortExpression"]) //如果为当前排序列
        {
            SortDirection = (kjkm_dg.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");     //获得下一次的排序状态
        }
        kjkm_dg.Attributes["SortExpression"] = SortExpression;
        kjkm_dg.Attributes["SortDirection"] = SortDirection;
        mikecatbind();
    }


类别:网上转载技术类 查看评论

Link URL: http://hi.baidu.com/yanzhiyong/blog/item/231b377fe07fe80b29388a46.html

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2019-12-12  
1234567
891011121314
15161718192021
22232425262728
293031    

数据统计

  • 访问量: 3247
  • 日志数: 30
  • 建立时间: 2009-03-02
  • 更新时间: 2009-07-29

RSS订阅

Open Toolbar