• 在GridView中进行排序


          点击列头,可以将列字段按照升降序进行排序。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                    ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <Columns>
                        <asp:BoundField DataField="UserID" HeaderText="用户ID" ReadOnly="True" SortExpression="UserID"/>
                        <asp:BoundField DataField="User_Nm" HeaderText="用户姓名" SortExpression="User_Nm" />
                        <asp:BoundField DataField="User_Sex" HeaderText="性别" SortExpression="User_Sex"/>
                        <asp:BoundField DataField="User_Address" HeaderText="家庭住址" SortExpression="User_Address" />
                        <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                        <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                        <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                    </Columns>
                    <RowStyle ForeColor="#008066" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                </asp:GridView>

          

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class Default2 : System.Web.UI.Page
    {
        SqlConnection sqlcon;
        string strCon = "Data Source=.\\SQL2005;DataBase=Users;uid=sa;pwd=123";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "User_Nm";
                ViewState["OrderDire"] = "ASC";
                bind();
            }
        }

        public void bind()
        {
            string sqlstr = "SELECT TOP 10 * from Users";
            sqlcon = new SqlConnection (strCon );
            SqlDataAdapter myda = new SqlDataAdapter (sqlstr ,sqlcon );

            DataSet myds = new DataSet ();
            sqlcon .Open ();

            myda.Fill(myds, "Us");
            DataView view = myds.Tables["Us"].DefaultView;
            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            view.Sort = sort;
            GridView1.DataSource = view;
            GridView1.DataBind();
            sqlcon.Close();

        }
    protected void  GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sPage = e.SortExpression ;
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "DESC")
            {
                ViewState["OrderDire"] = "ASC";
            }
            else
            {
                ViewState["OrderDire"] = "DESC";
            }
        }
      
        bind();
    }
    }

  • 相关阅读:
    验证码帮助类
    UDP聊天
    SoapHeader的使用
    单条目选择控件
    Redis命令总结
    PHP解决抢购、秒杀、抢楼、抽奖等阻塞式高并发库存防控超量的思路方法
    Python3经典100道练习题004
    Python3经典100道练习题001
    tkinter笔记01创建第一GUI
    Python3经典100道练习题006
  • 原文地址:https://www.cnblogs.com/daiweixm/p/1525451.html
Copyright © 2020-2023  润新知