zoukankan      html  css  js  c++  java
  • GridView中实现双向排序

    1.aspx前台代码

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">
                            <Columns>
                            <asp:BoundField DataField="deptid" HeaderText="部门代码" SortExpression="deptid"/>
                            <asp:BoundField DataField="deptename" HeaderText="部门名称" SortExpression="deptename"/>
                            <asp:TemplateField SortExpression="BUID" HeaderText="BUID">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server"   Text='<%# Bind("BUID") %>' ToolTip='<%# DataBinder.Eval(Container.DataItem, "BUID") %>'></asp:Label>
                            </ItemTemplate>
                            </asp:TemplateField>
                            </Columns>
                            </asp:GridView>

    2.aspx.cs后台代码

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ViewState["SortOrder"] = "deptid";
                ViewState["OrderDire"] = "asc";
                BindGridView();
            }
        }

        protected void BindGridView()
        {
          
            DataSet ds = new DataSet();
            ds = MIS.Users.GetDept();

            DataView dv = ds.Tables[0].DefaultView;
            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            dv.Sort = sort;

            this.GridView1.DataSource = dv;

            this.GridView1.DataBind();

        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string vortExp = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == vortExp)
            {
                if ((string)ViewState["OrderDire"] == "desc")
                {
                    ViewState["OrderDire"] = "asc";
                }
                else if ((string)ViewState["OrderDire"] == "asc")
                {
                    ViewState["OrderDire"] = "desc";
                }
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            BindGridView();

        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindGridView();
        }

  • 相关阅读:
    Python 队列
    Python 知识要点:input 和 print
    android中ScrollView的setOnClickListener无效
    android.content.res.Resources$NotFoundException: String resource ID #0x1 原因及解决方案
    百度莱茨狗
    机器学习入门——Iris问题
    安卓调用百度地图网页版进行路径规划与导航,只需提供地名
    error:style attribute '@android:attr/windowEnterAnimation' not found 解决方法
    逻辑结构,存储结构,物理结构的区分
    CSDN博客内容中嵌入其他网页
  • 原文地址:https://www.cnblogs.com/ly5201314/p/1390078.html
Copyright © 2011-2022 走看看