winform开发是或多或少都会接触datagridview控件,如果数据量大,那么必须使用分页功能,但是datagridview自身并没有分页,所以我们要自己实现。在网上搜了一些发现没有太适合自己的,要么嫌代码过于多不想看,要么自己理解差无法明白其原理,所以打算自己写一个,也希望帮到大家。
第一步:设置4个变量分别记录每页记录数、总记录数、总页数、当前页
/// <summary> /// 每页记录数 /// </summary> public int pageSize = 100; /// <summary> /// 总记录数 /// </summary> public int recordCount = 0; /// <summary> /// 总页数 /// </summary> public int pageCount = 0; /// <summary> /// 当前页 /// </summary> public int currentPage = 0;
第二步:构造一个虚拟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 = 1; i <= 30000; 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) > 0) { pageCount++; } //默认第一页 currentPage = 1; LoadPage( );//调用加载数据的方法 }
第三步:进行加载显示数据
/// <summary> /// LoadPage方法 /// </summary> private void LoadPage( ) { if (currentPage < 1) currentPage = 1; if (currentPage > pageCount) currentPage = pageCount; int beginRecord; //开始指针 int endRecord; //结束指针 DataTable dtTemp; dtTemp = table.Clone(); beginRecord = pageSize * (currentPage - 1); if (currentPage == 1) beginRecord = 0; 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 = 0; i < dtTemp.Rows.Count; i++) { dataGridView1.Rows.Add(new object[] { dtTemp.Rows[i][0] , dtTemp.Rows[i][1] , dtTemp.Rows[i][2] }); } 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 == 1) { return; } currentPage = 1; LoadPage(); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrev_Click(object sender, EventArgs e) { if (currentPage == 1) { 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();//分页 }
最终展示结果
最后附上源码地址
链接: https://pan.baidu.com/s/1J4qI5RZ2AhV3ycml2a2htA
提取码: 49ie