zoukankan      html  css  js  c++  java
  • GridView 自带排序分页


    //后台

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "Id";
                ViewState["OrderDire"] = "ASC";
                BindGridview();
            }
        }
        public void BindGridview()
        {
            try
            {
                SqlConnection conn = new SqlConnection("server=localhost;database=ExamDataBase;uid=sa;pwd=sa");
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter("ExamDataTable_GetAll", conn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);

                DataView view = dt.DefaultView;
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;

                WorkGuideGridView.DataSource = view;
                WorkGuideGridView.DataBind();
            }
            catch
            { }
        }
       //分页
        protected void WorkGuideGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            WorkGuideGridView.PageIndex = e.NewPageIndex;
            BindGridview();
        }
       //双向排序
        protected void WorkGuideGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            string vortExp = e.SortExpression;

            if (ViewState["SortOrder"].ToString() == vortExp)
            {
                if ((string)ViewState["OrderDire"] == "Desc")
                {
                    ViewState["OrderDire"] = "ASC";
                }
                else
                {
                    ViewState["OrderDire"] = "Desc";
                }
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            BindGridview();
        }


    HTML
    <asp:GridView ID="WorkGuideGridView" runat="server" AutoGenerateColumns="False" OnRowCommand="WorkGuideGridView_RowCommand"  Width="100%" PageSize="5" AllowPaging="True" AllowSorting="True"       OnPageIndexChanging="WorkGuideGridView_PageIndexChanging" OnSorting="WorkGuideGridView_Sorting">
                                <Columns>
                                    <asp:TemplateField HeaderText="ID" Visible="False">
                                        <ItemTemplate>
                                            <asp:Label ID="lblID" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="姓名" SortExpression="Name" ItemStyle-HorizontalAlign=center>
                                        <ItemTemplate>
                                            <asp:Label ID="TitleLabel" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="年龄" SortExpression="Age" ItemStyle-HorizontalAlign=center>
                                        <ItemTemplate>
                                            <asp:Label ID="TitleLabe2" runat="server" Text='<%#Bind("Age") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="编辑">
                                        <ItemTemplate>
                                            <asp:Button ID="EditButton" runat="server" Text="编辑" Width="50" CommandArgument="Edit" />
                                        </ItemTemplate>
                                        <ItemStyle Width="50px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="删除">
                                        <ItemTemplate>
                                            <asp:Button ID="DeleteButton" runat="server" Text="删除" Width="50" CommandArgument="Delete"
                                                OnClientClick="return delconfirm()" CssClass="ButtonCss" />
                                        </ItemTemplate>
                                        <ItemStyle Width="50px" />
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataTemplate>
                                    <asp:Label ID="EmptyLabel" runat="server" Text="没有记录!" CssClass="error"></asp:Label>
                                </EmptyDataTemplate>
                            </asp:GridView>

  • 相关阅读:
    2019 蓝桥杯国赛 B 组模拟赛 D. 程序设计:公约数
    2019长安大学ACM校赛网络同步赛 M LCM (数论)
    CCPC-Wannafly Winter Camp Day1 (Div2, onsite)
    CCPC-Wannafly Winter Camp Day1 流流流动 (树形dp)
    CCPC-Wannafly Winter Camp Day1 (Div2) 吃豆豆 (DP)
    CCPC-Wannafly Winter Camp Day1 (Div2, onsite) 夺宝奇兵
    CCPC-Wannafly Winter Camp Day1 爬爬爬山 (最短路)
    Comet OJ
    Comet OJ
    hdu-Danganronpa(AC自动机)
  • 原文地址:https://www.cnblogs.com/zhc088/p/860156.html
Copyright © 2011-2022 走看看