winform中DataGridView实现分页功能
Posted 260250932
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform中DataGridView实现分页功能相关的知识,希望对你有一定的参考价值。
http://liyaguang20111105.blog.163.com/blog/static/19929420220146283255809/
在winform的设计中,要实现对DataGridView控件的分页功能,需要两个控件:BindingSource、BindingNavigator,根据需求可对BindingNavigator进行自由的扩展,下图的示例则是根据一般需求对分页功能的实现。红色区域是对BindingNavigator控件扩展后的效果。
//窗体构造方法中定义分页所需变量:
int pageSize = 0; //每页显示行数
int nMax = 0; //总记录数
int pageCount = 0; //页数=总记录数/每页显示行数
int pageCurrent = 0; //当前页号
int nCurrent = 0; //当前记录行
DataTable dtInfo = new DataTable(); //存取查询数据结果
//分页功能实现
public void InitDataSet()
{
//判断每页显示记录数是否为空,在初始话窗体时为真
if (txtRecordNumOfPage.Text.Trim() == "")
{
try
{
//pageSize = Convert.ToInt16(ConfigurationManager.AppSettings["PageSize"]); //设置页面行数
//读取配置文件中设置的每页显示条数
string szConfigFileName = Application.ExecutablePath + ".config";
XmlDocument doc = new XmlDocument();
doc.Load(szConfigFileName);
XmlNode root = doc.SelectSingleNode("configuration");
XmlNode node = root.SelectSingleNode("appSettings/add[@key=‘PageSize‘]");
XmlElement el = node as XmlElement;
pageSize = Convert.ToUInt16(el.GetAttribute("value"));
}
catch
{
}
if (pageSize == 0)
{
pageSize = 20; //如果读取配置文件失败,则默认将每页显示条数设置为20
}
txtRecordNumOfPage.Text = pageSize.ToString(); //界面显示的“每页记录数”赋值
}
else
{
//读取界面设置的每页显示条数
pageSize = Convert.ToUInt16(txtRecordNumOfPage.Text.Trim());
}
//总记录数赋值
nMax = dtInfo.Rows.Count;
pageCount = (nMax / pageSize); //采用整除计算页数
//判断整除后是否有余数,有则对页数进行+1
if ((nMax % pageSize) > 0) pageCount++;
pageCurrent = 1; //当前页数从1开始
nCurrent = 0; //当前记录数从0开始
//调用显示数据方法
LoadData();
}
//显示数据方法
private void LoadData()
{
int nStartPos = 0; //当前页面开始记录行
int nEndPos = 0; //当前页面结束记录行
//判断查询结果是否为空
if (dtInfo.Rows.Count == 0)
{
dgvExperInfo.DataSource = null;
return;
}
else
{
DataTable dtTemp = dtInfo.Clone(); //克隆DataTable结构,即将字段名称进行复制
if (pageCurrent == 1)
{
bindingNavigatorMoveFirstPage.Enabled = false;
bindingNavigatorMovePreviousPage.Enabled = false;
}
else
{
bindingNavigatorMoveFirstPage.Enabled = true;
bindingNavigatorMovePreviousPage.Enabled = true;
}
if (pageCurrent == pageCount)
{
nEndPos = nMax;
bindingNavigatorMoveLastPage.Enabled = false;
bindingNavigatorMoveNextPage.Enabled = false;
}
else
{
bindingNavigatorMoveLastPage.Enabled = true;
bindingNavigatorMoveNextPage.Enabled = true;
nEndPos = pageSize * pageCurrent;
}
nStartPos = nCurrent;
lblPageCount.Text = pageCount.ToString(); //界面显示总页数
lblCurrentPage.Text = Convert.ToString(pageCurrent);//当前页数
txtCurrentPage.Text = Convert.ToString(pageCurrent);//跳转到页数的显示
//从元数据源复制记录行
for (int i = nStartPos; i < nEndPos; i++)
{
dtTemp.ImportRow(dtInfo.Rows[i]);
nCurrent++;
}
bdsInfo.DataSource = dtTemp;
bdnInfo.BindingSource = bdsInfo;
dgvExperInfo.DataSource = bdsInfo;
dgvExperInfo.ClearSelection();
}
}
//BindingNavigator控件上的项目点击事件,通过配置各个Item的Text值进行判断执行
private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "上一页")
{
pageCurrent--;
if (pageCurrent <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看!");
pageCurrent++;
return;
}
else
{
nCurrent = pageSize * (pageCurrent - 1);
}
LoadData();
}
if (e.ClickedItem.Text == "下一页")
{
pageCurrent++;
if (pageCurrent > pageCount)
{
MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
pageCurrent--;
return;
}
else
{
nCurrent=pageSize*(pageCurrent-1);
}
LoadData();
}
if (e.ClickedItem.Text == "首页")
{
pageCurrent = 1;
nCurrent = 0;
LoadData();
}
if (e.ClickedItem.Text == "尾页")
{
pageCurrent = pageCount;
nCurrent = pageSize * (pageCurrent - 1);
LoadData();
}
}
//跳转页实现
private void btnPage_Click(object sender, EventArgs e)
{
if (txtCurrentPage.Text.Trim() != "")
{
pageCurrent = Convert.ToInt16(txtCurrentPage.Text.Trim());
//若输入页号大于最大显示页号,则跳转至最大页
if (pageCurrent > pageCount)
{
pageCurrent = pageCount;
nCurrent = pageSize * (pageCurrent - 1);
}
//若输入页号小于1,则跳转至第一页
else if (pageCurrent < 1)
{
pageCurrent = 1;
nCurrent = 0;
LoadData();
}
//跳转至输入页号
else
{
nCurrent = pageSize * (pageCurrent - 1); //当前行数定位
}
//调用加载数据方法
LoadData();
}
}
//当前页输入字符限制
private void txtCurrentPage_TextChanged(object sender, EventArgs e)
{
bool IsNum = true;
foreach (char c in txtCurrentPage.Text.Trim())
{
if (!char.IsNumber(c)) { IsNum = false; break; }
}
if (IsNum == false)
{
txtCurrentPage.Text = pageCurrent.ToString();
}
}
//当前页回车事件调用跳转页的操作
private void txtCurrentPage_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btnPage_Click(sender,e);
}
}
//每页显示记录数变更事件
private void txtRecordNumOfPage_TextChanged(object sender, EventArgs e)
{
bool IsNum = true;
//输入字符限制
foreach (char c in txtRecordNumOfPage.Text.Trim())
{
if (!char.IsNumber(c)) { IsNum = false; break; }
}
if (IsNum == false)
{
txtRecordNumOfPage.Text = pageSize.ToString();
}
//判断输入的每页显示条数是否为空或是否为0,输入长度是否大于4位等情况
if (txtRecordNumOfPage.Text.Trim() == "" || Convert.ToUInt32(txtRecordNumOfPage.Text.Trim()) == 0 || txtRecordNumOfPage.Text.Trim().Length > 4)
{
txtRecordNumOfPage.Text = pageSize.ToString();
}
//规避了特殊情况后直接调用显示数据方法
LoadDocInfoToDGV();
}
C# WinForm中DataGirdView简单分页功能 1.定义变量 int pageSize = 0; //每页显示行数 int nMax = 0; //总记录数 int pageCount = 0; //页数=总记录数/每页显示行数 int pageCurrent = 0; //当前页号 int nCurrent = 0; //当前记录行 PS (在bdnInfo中添加上一页, 下一页, lblPageCountText, txtCurrentPage.Text) 2.在窗体内分别添加BindingNavigator (bdnInfo), BindingSource (bdsInfo), DataGirdView1 (dgvInfo) 3.绑定dgvInfo数据 DataSet ds = new DataSet(); ds = userBLL.GetModel(); DaTaTable dt = new DaTaTable (); dt = ds.Tables[0]; this.dgvInfo.DataSource = dt; InitDataSet(); 4.初始化dgvInfo private void InitDataSet() { pageSize = 10; //设置页面行数 nMax = dt.Rows.Count; pageCount = (nMax / pageSize); //计算出总页数 if ( (nMax % pageSize) > 0) { pageCount++; } pageCurrent = 1; //当前页数从1开始 nCurrent = 0; //当前记录数从0开始 LoadData(); } 5.加载数据 private void LoadData() { int nStartPos = 0; //当前页面开始记录行 int nEndPos = 0; //当前页面结束记录行 System.Data.DataTable dtTemp = dt.Clone(); //克隆DataTable结构框架 if (dtTemp != null || dtTemp.Rows.Count > 0) { if (pageCurrent == pageCount) { nEndPos = nMax; } else { nEndPos = pageSize * pageCurrent; } nStartPos = nCurrent; lblPageCount.Text = pageCount.ToString();//在bdnInfo中添加lable记录总页数 txtCurrentPage.Text = Convert.ToString (pageCurrent); //在bdnInfo中添加T |
以上是关于winform中DataGridView实现分页功能的主要内容,如果未能解决你的问题,请参考以下文章
使用Entity Framework时如何在Winform场景下实现DataGridView的分页?
我们如何在winform的datagridview中进行分页
winform ListView和DataGridView实现分页
winform中的DataGridView如何实现分页(C#)