C# datagridview分页功能

  winform开发是或多或少都会接触datagridview控件,如果数据量大,那么必须使用分页功能,但是datagridview自身并没有分页,所以我们要自己实现。在网上搜了一些发现没有太适合自己的,要么嫌代码过于多不想看,要么自己理解差无法明白其原理,所以打算自己写一个,也希望帮到大家。

第一步:设置4个变量分别记录每页记录数、总记录数、总页数、当前页

        /// <summary>
/// 每页记录数
/// </summary>
public int pageSize = ; /// <summary>
/// 总记录数
/// </summary>
public int recordCount = ; /// <summary>
/// 总页数
/// </summary>
public int pageCount = ; /// <summary>
/// 当前页
/// </summary>
public int currentPage = ;

第二步:构造一个虚拟table用于显示(这里不连接数据库,如果需要自行更改),并算出总页数、总记录数。

DataTable table = new DataTable();

/// <summary>
/// 分页的方法
/// </summary>
/// <param name="str"></param>
private void PageSorter()
{ //创建虚拟表
DataColumn column1 = new DataColumn("test1", Type.GetType("System.String"));
DataColumn column2 = new DataColumn("test2", Type.GetType("System.String"));
DataColumn column3 = new DataColumn("test3", Type.GetType("System.String")); table.Columns.Add(column1); //将列添加到table表中
table.Columns.Add(column2);
table.Columns.Add(column3);
for (int i = ; i <= ; i++)
{
DataRow dr = table.NewRow(); //table表创建行
dr["test1"] = "资产编号" + i.ToString();
dr["test2"] = "资产名称" + i.ToString();
dr["test3"] = "规格型号" + i.ToString();
table.Rows.Add(dr); //将数据加入到table表中
} recordCount = table.Rows.Count; //记录总行数
pageCount = (recordCount / pageSize);
if ((recordCount % pageSize) > )
{
pageCount++;
} //默认第一页
currentPage = ; LoadPage( );//调用加载数据的方法
}

第三步:进行加载显示数据

/// <summary>
/// LoadPage方法
/// </summary>
private void LoadPage( )
{
if (currentPage < ) currentPage = ;
if (currentPage > pageCount) currentPage = pageCount; int beginRecord; //开始指针
int endRecord; //结束指针
DataTable dtTemp;
dtTemp = table.Clone(); beginRecord = pageSize * (currentPage - );
if (currentPage == ) beginRecord = ;
endRecord = pageSize * currentPage; if (currentPage == pageCount) endRecord = recordCount;
for (int i = beginRecord; i < endRecord; i++)
{
dtTemp.ImportRow(table.Rows[i]);
} dataGridView1.Rows.Clear(); for (int i = ; i < dtTemp.Rows.Count; i++)
{
dataGridView1.Rows.Add(new object[] { dtTemp.Rows[i][] , dtTemp.Rows[i][] , dtTemp.Rows[i][] });
} labPageIndex.Text = "当前页: " + currentPage.ToString() + " / " + pageCount.ToString();//当前页
labRecordCount.Text = "总行数: " + recordCount.ToString() + " 行";//总记录数
}

第四步:设置按钮点击事件

/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
if (currentPage == )
{ return; }
currentPage = ;
LoadPage();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrev_Click(object sender, EventArgs e)
{
if (currentPage == )
{ return; }
currentPage--;
LoadPage();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
if (currentPage == pageCount)
{ return; }
currentPage++;
LoadPage();
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
if (currentPage == pageCount)
{ return; }
currentPage = pageCount;
LoadPage();
}

第五步:load事件加载时显示数据

private void Form1_Load(object sender, EventArgs e)
{
PageSorter();//分页
}

最终展示结果

C# datagridview分页功能

最后附上源码地址

  链接: https://pan.baidu.com/s/1J4qI5RZ2AhV3ycml2a2htA

  提取码: 49ie

上一篇:转-阿里云Kubernetes容器Istio实践之集成日志


下一篇:javascript – 如何播放我从websocket流收到的PCM音频?