主题:[原创]<原创>使用BindingSource对DataGridView进行分页
原创代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BindingSourceTest
{
public partial class Form1 : Form
{
int row = 0; //每页显示行数
int currentPage = 0; //当前页号
int rowCount = 0; //当前记录行
int sum = 0; //总记录数
int pageCount = 0; //页数=总记录数/每页显示行数
public Form1()
{
InitializeComponent();
}
//DataSet ds = new DataSet();
DataTable table = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=北风贸易;integrated security=sspi;");--Data Source后面是服务器名称,Initial Catalog后面是数据库名称,我用的数据库叫北风贸易,最后面那句是固定的
SqlDataAdapter cmd = new SqlDataAdapter("select * from 章立民研究室", con);--章立民研究室是我用的表名
cmd.Fill(table);
InitDataSet();
}
private void InitDataSet()
{
row = 20; //设置页面行数
sum = table.Rows.Count; //总行数
pageCount = (sum / row); //计算出总页数
if ((sum % row) > 0) pageCount++;
currentPage = 1; //当前页数从1开始
rowCount = 0; //当前记录数从0开始
LoadData();
}
private void LoadData()
{
int Start = 0; //当前页面开始记录行
int End = 0; //当前页面结束记录行
DataTable tableClone = table.Clone(); //克隆DataTable结构框架
#region 设置按钮的可用
if (currentPage <= 1)
{
toolStripLabel2.Enabled = false;
}
else
{
toolStripLabel2.Enabled = true;
}
if (currentPage >= pageCount)
{
toolStripLabel3.Enabled = false;
}
else
{
toolStripLabel3.Enabled = true;
}
#endregion
if (currentPage == pageCount)
End = sum;
else
End = row * currentPage;
Start = rowCount;
lblPageCount.Text = pageCount.ToString();
txtCurrentPage.Text = Convert.ToString(currentPage);
//从元数据源复制记录行
for (int i = Start; i < End; i++)
{
tableClone.ImportRow(table.Rows[i]);
rowCount++;
}
bdsInfo.DataSource = tableClone;
bdnInfo.BindingSource = bdsInfo;
dgvInfo.DataSource = bdsInfo;
}
DataGridView上的ItemClick事件
private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "上一页")
{
currentPage--;
if (currentPage <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看!");
return;
}
else
{
rowCount = row * (currentPage - 1);
}
LoadData();
}
if (e.ClickedItem.Text == "下一页")
{
currentPage++;
if (currentPage > pageCount)
{
MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
return;
}
else
{
rowCount = row * (currentPage - 1);
}
LoadData();
}
}
}
}
注意:bdsInfo 是BindingSource
bdnInfo 是BindingNavigator
dgvInfo 是DataGridView
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BindingSourceTest
{
public partial class Form1 : Form
{
int row = 0; //每页显示行数
int currentPage = 0; //当前页号
int rowCount = 0; //当前记录行
int sum = 0; //总记录数
int pageCount = 0; //页数=总记录数/每页显示行数
public Form1()
{
InitializeComponent();
}
//DataSet ds = new DataSet();
DataTable table = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=北风贸易;integrated security=sspi;");--Data Source后面是服务器名称,Initial Catalog后面是数据库名称,我用的数据库叫北风贸易,最后面那句是固定的
SqlDataAdapter cmd = new SqlDataAdapter("select * from 章立民研究室", con);--章立民研究室是我用的表名
cmd.Fill(table);
InitDataSet();
}
private void InitDataSet()
{
row = 20; //设置页面行数
sum = table.Rows.Count; //总行数
pageCount = (sum / row); //计算出总页数
if ((sum % row) > 0) pageCount++;
currentPage = 1; //当前页数从1开始
rowCount = 0; //当前记录数从0开始
LoadData();
}
private void LoadData()
{
int Start = 0; //当前页面开始记录行
int End = 0; //当前页面结束记录行
DataTable tableClone = table.Clone(); //克隆DataTable结构框架
#region 设置按钮的可用
if (currentPage <= 1)
{
toolStripLabel2.Enabled = false;
}
else
{
toolStripLabel2.Enabled = true;
}
if (currentPage >= pageCount)
{
toolStripLabel3.Enabled = false;
}
else
{
toolStripLabel3.Enabled = true;
}
#endregion
if (currentPage == pageCount)
End = sum;
else
End = row * currentPage;
Start = rowCount;
lblPageCount.Text = pageCount.ToString();
txtCurrentPage.Text = Convert.ToString(currentPage);
//从元数据源复制记录行
for (int i = Start; i < End; i++)
{
tableClone.ImportRow(table.Rows[i]);
rowCount++;
}
bdsInfo.DataSource = tableClone;
bdnInfo.BindingSource = bdsInfo;
dgvInfo.DataSource = bdsInfo;
}
DataGridView上的ItemClick事件
private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "上一页")
{
currentPage--;
if (currentPage <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看!");
return;
}
else
{
rowCount = row * (currentPage - 1);
}
LoadData();
}
if (e.ClickedItem.Text == "下一页")
{
currentPage++;
if (currentPage > pageCount)
{
MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
return;
}
else
{
rowCount = row * (currentPage - 1);
}
LoadData();
}
}
}
}
注意:bdsInfo 是BindingSource
bdnInfo 是BindingNavigator
dgvInfo 是DataGridView