winform中datagridview的分页器设计

   winform中的datagridview没有分页功能,如果要显示的数据较多还需要往下拖动滚动条,比较麻烦。自己动手制作一个简单的分页器即可解决这个问题。分页器如下:

winform中datagridview的分页器设计
废话不多说,直接上代码,分页器的代码如下:

public partial class PageControl : UserControl
{
public delegate void EventPagingHandler(EventArgs e);
public PageControl()
{
InitializeComponent();
}
public event EventPagingHandler EventPaging;
#region 公开属性
private int _pageSize = 50;
///
/// 每页显示记录数(默认50)
///
public int PageSize
{
get
{
return _pageSize;
}
set
{
if (value > 0)
{
_pageSize = value;
}
else
{
_pageSize = 50;
}
this.comboPageSize.Text = _pageSize.ToString();
}
}
private int _currentPage = 1;
///
/// 当前页
///
public int CurrentPage
{
get
{
return _currentPage;
}
set
{
if (value > 0)
{
_currentPage = value;
}
else
{
_currentPage = 1;
}

        }
    }
    private int _totalCount = 0;
    /// <summary>
    /// 总记录数
    /// </summary>
    public int TotalCount
    {
        get
        {
            return _totalCount;
        }
        set
        {
            if (value >= 0)
            {
                _totalCount = value;
            }
            else
            {
                _totalCount = 0;
            }
            this.lblTotalCount.Text = this._totalCount.ToString();
            CalculatePageCount();
            //this.lblRecordRegion.Text = GetRecordRegion();
        }
    }

    private int _pageCount = 0;
    /// <summary>
    /// 页数
    /// </summary>
    public int PageCount
    {
        get
        {
            return _pageCount;
        }
        set
        {
            if (value >= 0)
            {
                _pageCount = value;
            }
            else
            {
                _pageCount = 0;
            }
            this.lblPageCount.Text = _pageCount + "";
        }
    }
    #endregion

    /// <summary>
    /// 计算页数
    /// </summary>
    private void CalculatePageCount()
    {
        if (this.TotalCount > 0)
        {
            this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.TotalCount) / Convert.ToDouble(this.PageSize)));
        }
        else
        {
            this.PageCount = 0;
        }
    }
    public void Bind()
    {
        if (this.EventPaging != null)
        {
            this.EventPaging(new EventArgs());
        }
        if (this.CurrentPage > this.PageCount)
        {
            this.CurrentPage = this.PageCount;
        }
        this.txtBoxCurPage.Text = this.CurrentPage + "";
        this.lblTotalCount.Text = this.TotalCount + "";
        this.lblPageCount.Text = this.PageCount + "";
    }

   private void btn_Last_Click(object sender, EventArgs e)
    {
        this.CurrentPage -= 1;
        this.Bind();
    }
    private void btn_Next_Click(object sender, EventArgs e)
    {
        this.CurrentPage += 1;
        this.Bind();
    }

    private void btn_Top_Click(object sender, EventArgs e)
    {
        this.CurrentPage = 1;
        this.Bind();
    }

    private void btn_End_Click(object sender, EventArgs e)
    {
        this.CurrentPage = this.PageCount;
        this.Bind();
    }

    private void comboPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.PageSize = Convert.ToInt32 (comboPageSize.Text);
        this.Bind();
    }

}
设计完分页器,重新生成项目,在工具箱中可以看到刚在设计的分页器控件PageControl,该用户控件可以像其他普通控件一下拖拽使用。
在需要使用该分页器的form的.cs中的构造函数中添加如下代码:
pageControl1.EventPaging += new PageControl.EventPagingHandler(paging_EventPaging);
pageControl1是分页器的名称,paging_EventPaging为分页器需要绑定的事件,paging_EventPaging事件代码如下:
private void paging_EventPaging(EventArgs e)
 {
        ExcuteChaXun("", "", 0);
 }
 在ExcuteChaXun中为PageControl控件的TotalCount 和PageCount 属性赋值:
 List<ProjectModel> ProjectList = Reflector.ConvertTableToList<ProjectModel>(data).OrderByDescending(t => t.Time).ToList() ;
            pageControl1.TotalCount = ProjectList.Count;
            pageControl1.PageCount = (ProjectList.Count % pageControl2.PageSize == 0) ? (ProjectList.Count / pageControl2.PageSize) : (ProjectList.Count / pageControl2.PageSize + 1);
上一篇:ADO SQL手写分页


下一篇:IOS- 简单说说UIApplication