none
Gridview分页 RRS feed

  • 问题

  • 大家好,
    我想知道如何在grdiview中实现分页功能,我要那种下拉菜单的,就是可以点击1显示1,点击2显示2,点击3显示3.
    2016年10月27日 9:08

全部回复

  • 你好,

    按照你的描述,我建议你可以在后台自定义一个绑定girdiview的方法,然后通过特定的SQL查询语句,来根据你选中的下拉框的值来显示grdiview中的内容。

    你可以使用一个存储过程,存储过程的代码如下:

    这里把所有的行数返回出去用于设置下拉框的最大值

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [dbo].[spx_GetCustomers]
           @TotalRows INT OUTPUT
    AS
    BEGIN
          SET NOCOUNT ON;
          SELECT ContactName, City, Country, PostalCode
          FROM Customers
          SELECT @TotalRows=COUNT(*)
          FROM CUSTOMERS
    END

    具体代码如下:

    grdiview:

    <div>
    跳转到:
    <asp:DropDownList ID="ddlJumpTo" runat="server"
        OnSelectedIndexChanged = "PageNumberChanged" AutoPostBack = "true">
    </asp:DropDownList>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
        AllowPaging = "true" PageSize = "10" PagerSettings-Visible = "false">
        <Columns>
            <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
            <asp:BoundField DataField="City" HeaderText="City" />
            <asp:BoundField DataField="Country" HeaderText="Country" />
            <asp:BoundField DataField="PostalCode" HeaderText="Postal Code" />
        </Columns>
    </asp:GridView>
    </div>

    后台:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int TotalRows = this.BindGrid(1);
            this.FillJumpToList(TotalRows);
        }
    }
    
    private int BindGrid(int CurrentPageNo)
    {
        int TotalRows = 0;
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager
                    .ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand("spx_GetCustomers");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@TotalRows", SqlDbType.Int)
                    .Direction = ParameterDirection.Output;
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        TotalRows = (int)cmd.Parameters["@TotalRows"].Value;
        GridView1.PageIndex = CurrentPageNo - 1;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        return TotalRows;
    }
    private void FillJumpToList(int TotalRows)
    {
        int PageCount = this.CalculateTotalPages(TotalRows);
        for (int i = 1; i <= PageCount; i++)
        {
            ddlJumpTo.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }
    private int CalculateTotalPages(int intTotalRows)
    {
        int intPageCount = 1;
        double dblPageCount = (double)(Convert.ToDecimal(intTotalRows)
                                / Convert.ToDecimal(GridView1.PageSize));
        intPageCount = Convert.ToInt32(Math.Ceiling(dblPageCount));
        return intPageCount;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int TotalRows = this.BindGrid(1);
            this.FillJumpToList(TotalRows);
        }
    }
    


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年10月28日 2:42
  • 你好 Anne_Wx,

    这个问题解决了吗?现在有什么进展? 如果没有解决,请告诉我们你的问题,微软ASP.NET 专家会帮助解决。

    Best regards,

    yanjin 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年11月11日 9:50