Winform自定义分页控件的实现

时间:2023-03-08 16:37:52
Winform自定义分页控件的实现

实现效果 有点丑陋 但是功能是没问题的 测试过

Winform自定义分页控件的实现

实现思路

先创建一个用户控件

Winform自定义分页控件的实现

代码实现

 public partial class PagerControl : UserControl
{
private int record = ; /// <summary>
/// 总记录数
/// </summary>
public int Record
{
get { return record; }
set
{
record = value;
InitPageInfo();
}
} private int pageSize = ; /// <summary>
/// 每页条数
/// </summary>
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
} private int currentPage = ; /// <summary>
/// 当前页
/// </summary>
public int CurrentPage
{
get { return currentPage; }
set { currentPage = value; }
} public int pageNum = ; /// <summary>
/// 总页码
/// </summary>
public int PageNum
{
get
{
if (Record == )
{
pageNum = ;
}
else
{
if (Record % PageSize > )
{
pageNum = Record / PageSize + ;
}
else
{
pageNum = Record / PageSize;
}
}
return pageNum;
} } //定义委托
public delegate void BindHandle(object sender, EventArgs e); /// <summary>
/// 绑定数据源事件
/// </summary>
public event BindHandle BindSource; public PagerControl()
{
InitializeComponent();
} /// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
if (Record > )
{
if (CurrentPage == )
{
MessageBox.Show("当前已经是首页");
return;
}
else
{
CurrentPage = ;
if (BindSource != null)
{
BindSource(sender, e);
InitPageInfo();
}
}
} } private void btnPre_Click(object sender, EventArgs e)
{
if (Record > )
{
if (CurrentPage == )
{
MessageBox.Show("当前已经是首页");
return;
}
else
{
CurrentPage = CurrentPage - ;
if (BindSource != null)
{
BindSource(sender, e);
InitPageInfo();
}
}
}
} private void btnNext_Click(object sender, EventArgs e)
{
if (Record > )
{
if (CurrentPage == PageNum)
{
MessageBox.Show("当前已经是末页");
return;
}
else
{
CurrentPage = CurrentPage + ;
if (BindSource != null)
{
BindSource(sender, e);
InitPageInfo();
}
}
}
} private void btnLast_Click(object sender, EventArgs e)
{
if (Record > )
{
if (CurrentPage == PageNum)
{
MessageBox.Show("当前已经是末页");
return;
}
else
{
CurrentPage = PageNum;
if (BindSource != null)
{
BindSource(sender, e);
InitPageInfo();
}
}
}
}  private void InitPageInfo()
        {
             if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
            {
                CurrentPage = 1;
            }
            lblInfo.Text = string.Format("共 {0} 条记录  共 {1} 页  当前第 {2} 页", Record, PageNum, CurrentPage);
            txtPage.Text = CurrentPage.ToString();         } private void btnGo_Click(object sender, EventArgs e)
{
if (Record > )
{
if (!string.IsNullOrEmpty(txtPage.Text) && !Regex.IsMatch(txtPage.Text, @"^[\d]*$"))
{
MessageBox.Show("请正确填写页码!");
return;
}
int page = Convert.ToInt32(txtPage.Text);
if (page == )
{
page = ;
}
if (page > PageNum)
{
page = PageNum;
} CurrentPage = page;
if (BindSource != null)
{
BindSource(sender, e);
InitPageInfo();
}
} } private void PagerControl_Load(object sender, EventArgs e)
{
if (BindSource != null)
{
BindSource(sender, e);
InitPageInfo();
}
}
}

使用

只要在窗体中 写好绑定方法

 private void Bind()
{
string start = dtpDate1.Value.ToString("yyyy-MM-dd");
string end = dtpDate2.Value.ToString("yyyy-MM-dd");
string team = cbxTeam.SelectedValue.ToString();
string jieshu = cbxSFJS.SelectedValue.ToString();
int record = ;
DataTable dt = eventBiz.GetEvents(start, end, team, jieshu, pagerControl1.CurrentPage, pagerControl1.PageSize,out record);
pagerControl1.Record = record; dgvEvent.AutoGenerateColumns = false;
dgvEvent.DataSource = dt.DefaultView;
}

捆绑绑定事件

 /// <summary>
/// 绑定数据源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pagerControl1_BindSource(object sender, EventArgs e)
{
Bind();
}

就可以了  需要注意的事情是由于分页控件load事件里会调用bind方法,会用到一些窗体元素的值,所以窗体元素项的初始化,应该放在窗体构造函数中,不要放在窗体load事件里。