博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GridView 自带排序分页

Posted on 2007-08-17 18:27  小飞龙(Jack)  阅读(536)  评论(0编辑  收藏  举报


//后台

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>