DataGridView翻页

来源:互联网 发布:win7 软件停止工作 编辑:程序博客网 时间:2024/04/30 15:58

。net翻页很简单,主要解决两个问题,记录当前页,绑定数据。

/// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtFirstPage_Click(object sender, EventArgs e)
    {
        lbtNextPage.Enabled = true;
        lbtLastPage.Enabled = true;

        lbtPreviewPage.Enabled = false;
        lbtFirstPage.Enabled = false;

        hfCurrentPage.Value = "1";
        ddlCurrenPage.SelectedValue = hfCurrentPage.Value;

        DBModule dbm = DBModule.Instance();

        GridViewLBJ.PageIndex = 0;
        GridViewLBJ.DataSource = dbm.DataTableOfLBJ;
        GridViewLBJ.DataBind();
    }

    /// <summary>
    /// 前一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtPreviewPage_Click(object sender, EventArgs e)
    { 
        lbtNextPage.Enabled = true;
        lbtLastPage.Enabled = true;

        int count = Convert.ToInt32(hfCurrentPage.Value);
        count--;
        hfCurrentPage.Value = count.ToString();
        ddlCurrenPage.SelectedValue = hfCurrentPage.Value;
        if (count == 1)
        {
            lbtPreviewPage.Enabled = false;
            lbtFirstPage.Enabled = false;
        }

        DBModule dbm = DBModule.Instance();

        GridViewLBJ.PageIndex = count - 1;
        GridViewLBJ.DataSource = dbm.DataTableOfLBJ;
        GridViewLBJ.DataBind();
    }

    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtNextPage_Click(object sender, EventArgs e)
    {   
        lbtPreviewPage.Enabled = true;
        lbtFirstPage.Enabled = true;

        int count = Convert.ToInt32(hfCurrentPage.Value);
        int sumCount = Convert.ToInt32(hfSumPage.Value);
        count++;
        hfCurrentPage.Value = count.ToString();
        ddlCurrenPage.SelectedValue = hfCurrentPage.Value;
        if (count == sumCount)
        {
            lbtNextPage.Enabled = false;
            lbtLastPage.Enabled = false;
        }

        DBModule dbm = DBModule.Instance();

        GridViewLBJ.PageIndex = count - 1;
        GridViewLBJ.DataSource = dbm.DataTableOfLBJ;
        GridViewLBJ.DataBind();
    }

    /// <summary>
    /// 末页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtLastPage_Click(object sender, EventArgs e)
    {
        lbtPreviewPage.Enabled = true;
        lbtFirstPage.Enabled = true;

        lbtNextPage.Enabled = false;
        lbtLastPage.Enabled = false;

        hfCurrentPage.Value = hfSumPage.Value;
        ddlCurrenPage.SelectedValue = hfCurrentPage.Value;

        DBModule dbm = DBModule.Instance();

        GridViewLBJ.PageIndex = Convert.ToInt32(hfSumPage.Value) - 1;
        GridViewLBJ.DataSource = dbm.DataTableOfLBJ;
        GridViewLBJ.DataBind();
    }