有时候我们需要进行分页显示,第一方面是在大数据量下可以降低卡顿,另一方面也是方便查找。
首先划重点,如果卡顿,不要用单元格填充的方式去刷新,用绑定数据源的方式比较高效!
下面重点讲如何使用数据源绑定数据的方式,对数据进行分页展示。
本文中配合使用了BindingNavigator控件,该控件有很多功能,因为开发时间比较紧急,下文中仅使用该控件进行按钮的替代。
效果如下:
目录
1.设置
按钮及对应名称如下:
ps:这个控件和工具栏很像,后续有时间我们再谈。
其他全局参数
csharp
DataTable dt_DevState = new DataTable("state");//源数据,大家自行获取(未分页前的所有数据)
public int pageSize = 5;//一页显示多少行
int pageCount = 0;
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
if (dt_DevState != null)
{
pageCount = (int)Math.Ceiling(dt_DevState.Rows.Count * 1.0 / pageSize);
}
return pageCount;
}
}
int currentPage = 1;
/// <summary>
/// 当前页
/// </summary>
public int CurrentPage
{
get { return currentPage; }
set
{
if (value <= 1)
{
value = 1;
this.bdFirstItem.Enabled = false;
this.bdPreviousItem.Enabled = false;
this.bdNextItem.Enabled = true;
this.bdLastItem.Enabled = true;
}
else if (value >= PageCount)
{
value = pageCount;
this.bdFirstItem.Enabled = true;
this.bdPreviousItem.Enabled = true;
this.bdNextItem.Enabled = false;
this.bdLastItem.Enabled = false;
}
else
{
this.bdFirstItem.Enabled = true;
this.bdPreviousItem.Enabled = true;
this.bdNextItem.Enabled = true;
this.bdLastItem.Enabled = true;
}
currentPage = value;
}
}
int currentRow;
/// <summary>
/// 每页首行索引
/// </summary>
public int CurrentRow
{
get
{
if (CurrentPage == 1)
{
currentRow = 0;
}
else
{
currentRow = (CurrentPage - 1) * pageSize;
}
return currentRow;
}
}
int max = 0;
/// <summary>
/// 每页行数最大值索引
/// </summary>
public int Max
{
get
{
if (CurrentPage == pageCount)
{
max = dt_DevState.Rows.Count;
}
else
{
max = pageSize * CurrentPage;
}
return max;
}
}
2.初始化
根据DataGridView控件大小,获取DataGridView每页显示的行数:
csharp
private void dataGridView1_SizeChanged(object sender, EventArgs e)
{
pageSize = (dataGridView1.Height-dataGridView1.ColumnHeadersHeight)/ dataGridView1.RowTemplate.Height;
BindPage();
}
绑定下拉框中的页码:
csharp
private void BindPage()
{
bindingNavigator1.Enabled = true;
this.bdCountItem.Text = PageCount.ToString();
this.tspcbo.Items.Clear();
for (int i = 1; i <= pageCount; i++)
{
this.tspcbo.Items.Add("第" + i + "页");
}
if(tspcbo.Items.Count>0)
this.tspcbo.SelectedIndex = 0;
}
3.按钮绑定事件
按钮按下后更新下拉列表控件的显示内容,实现分页显示。
csharp
private void tspcbo_SelectedIndexChanged(object sender, EventArgs e)
{
//下拉框选择不同页码更新表显示
CurrentPage = this.tspcbo.SelectedIndex + 1;
this.bdPositionItem.Text = CurrentPage.ToString();
if (!this.IsHandleCreated) return;
this.BeginInvoke(new EventHandler(delegate
{
UpdateTable();//更新数据,在步骤4中有代码
}));
}
private void bdFirstItem_Click(object sender, EventArgs e)
{
this.tspcbo.SelectedIndex = 0;
}
private void bdPreviousItem_Click(object sender, EventArgs e)
{
this.tspcbo.SelectedIndex = CurrentPage - 2;
}
private void bdNextItem_Click(object sender, EventArgs e)
{
this.tspcbo.SelectedIndex = CurrentPage;
}
private void bdLastItem_Click(object sender, EventArgs e)
{
this.tspcbo.SelectedIndex = PageCount - 1;
}
4.更新数据
csharp
public void UpdateTable()
{
if (!dataGridView1.Visible) return;
dataGridView1.SuspendLayout();
var dt_Page = BindSourceFY();
dataGridView1.DataSource = dt_Page.Copy();
dataGridView1.ResumeLayout();
}
/// <summary>
/// 分页绑定数据
/// </summary>
private DataTable BindSourceFY()
{
if (dt_DevState == null|| dt_DevState.Rows.Count==0) return dt_DevState;
DataTable dt_Page = dt_DevState.Clone();
for (int i = CurrentRow; i < Max; i++)
{
dt_Page.ImportRow(dt_DevState.Rows[i]);
}
return dt_Page;
}