• 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();
        }

  • 相关阅读:
    mac下xcode中include path
    如何联机调试和发布程序
    Facebook 错误码
    app store给应用评分功能
    FTP传输模式
    重新安装 Visual Studio 6.0 的问题
    ASP项目:提示用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败
    投资项目的4P
    .net 的资源与本地化
    千万不能干的事(文摘)
  • 原文地址:https://www.cnblogs.com/ly5201314/p/1390078.html
Copyright © 2020-2023  润新知