DataGridView使用BindingNavigator实现简单分页功能
上篇文章给大家介绍DataGridView使用自定义控件实现简单分页功能,本篇使用BindingNavigator来实现简单分页功能。其实也只是借用了一个BindingNavigator空壳,
实现原理和代码与上一篇几乎一样,实现方法如下:
1、新建一个WinForm程序,命名为BindingNavigatorMain,并拖入一个DataGridView控件及一个BindingNavigator控件。在BindingNavigator右下角弹窗中添加
一个Button(转到),BindingNavigator的样式如下:
2、BindingNavigatorMain的代码如下:
privateintpageSize;//每页显示记录数
privateintpageIndex;//页序号
privateinttotalCount;//总记录数
privateintpageCount;//总页数
publicBindingNavigatorMain()
{
InitializeComponent();
}
privatevoidBindingNavigatorMain_Load(objectsender,EventArgse)
{
pageSize=20;
pageIndex=0;
SetPage();
}
//设置页
privatevoidSetPage()
{
//总记录数
totalCount=0;
BindPage(pageSize,pageIndex+1,outtotalCount);
//总页数
if(totalCount%pageSize==0)
pageCount=totalCount/pageSize;
else
pageCount=totalCount/pageSize+1;
//当前页及总页数
txtCurrentPage.Text=(pageIndex+1).ToString();
lblTotalPage.Text="共"+pageCount.ToString()+"页";
//BindingNavigator数据源不进行BindingSource赋值,但恢复控件可用性。
bindingNavigatorMoveFirstItem.Enabled=true;
bindingNavigatorMovePreviousItem.Enabled=true;
txtCurrentPage.Enabled=true;
lblTotalPage.Enabled=true;
bindingNavigatorMoveNextItem.Enabled=true;
bindingNavigatorMoveLastItem.Enabled=true;
}
///
///绑定页
///
///每页显示记录数
///页序号
///总记录数
privatevoidBindPage(intpageSize,intpageIndex,outinttotalCount)
{
SqlConnectionconn=null;
SqlCommandcmd=null;
totalCount=0;
#region连接数据库测试
try
{
//数据库连接
conn=newSqlConnection("server=.;database=DB_TEST;Uid=sa;pwd=********;");
conn.Open();
//SqlCommand
cmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="PageTest";
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter[]param=
{
newSqlParameter("@PageSize",SqlDbType.Int),
newSqlParameter("@PageIndex",SqlDbType.Int),
newSqlParameter("@TotalCount",SqlDbType.Int)
};
param[0].Value=pageSize;
param[1].Value=pageIndex;
param[2].Direction=ParameterDirection.Output;
cmd.Parameters.AddRange(param);
//DataTable
DataTabledt=newDataTable("MF_MO");
dt.Columns.Add(newDataColumn("MO_NO",typeof(String)));
dt.Columns.Add(newDataColumn("MRP_NO",typeof(String)));
dt.Columns.Add(newDataColumn("QTY",typeof(Decimal)));
dt.Columns.Add(newDataColumn("BIL_NO",typeof(String)));
#region方法一:SqlDataReader
SqlDataReaderdr=cmd.ExecuteReader();
dt.Load(dr,LoadOption.PreserveChanges);
dr.Close();
totalCount=(int)param[2].Value;
dataGridView1.DataSource=dt;
#endregion
#region#方法二:SqlDataAdapter
//SqlDataAdapterda=newSqlDataAdapter();
//da.SelectCommand=cmd;
//dt.BeginLoadData();
//da.Fill(dt);
//dt.EndLoadData();
//totalCount=(int)param[2].Value;
//dataGridView1.DataSource=dt;
#endregion
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
conn.Close();
cmd.Dispose();
}
#endregion
}
///
///首页
///
///
///
privatevoidbindingNavigatorMoveFirstItem_Click(objectsender,EventArgse)
{
pageIndex=0;
SetPage();
}
///
///上一页
///
///
///
privatevoidbindingNavigatorMovePreviousItem_Click(objectsender,EventArgse)
{
pageIndex--;
if(pageIndex<0)
{
pageIndex=0;
}
SetPage();
}
///
///下一页
///
///
///
privatevoidbindingNavigatorMoveNextItem_Click(objectsender,EventArgse)
{
pageIndex++;
if(pageIndex>pageCount-1)
{
pageIndex=pageCount-1;
}
SetPage();
}
///
///末页
///
///
///
privatevoidbindingNavigatorMoveLastItem_Click(objectsender,EventArgse)
{
pageIndex=pageCount-1;
SetPage();
}
///
///只能按0-9、Delete、Enter、Backspace键
///
///
///
privatevoidtxtCurrentPage_KeyPress(objectsender,KeyPressEventArgse)
{
if((e.KeyChar>=48&&e.KeyChar<=57)||e.KeyChar==8||e.KeyChar==13||e.KeyChar==127)
{
e.Handled=false;
if(e.KeyChar==13)
{
Go();
}
}
else
{
e.Handled=true;
}
}
///
///指定页
///
///
///
privatevoidbtnGo_Click(objectsender,EventArgse)
{
Go();
}
privatevoidGo()
{
if(string.IsNullOrEmpty(txtCurrentPage.Text))
{
MessageBox.Show("指定页不能为空。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtCurrentPage.Focus();
return;
}
if(int.Parse(txtCurrentPage.Text)>pageCount)
{
MessageBox.Show("指定页已超过总页数。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtCurrentPage.Focus();
return;
}
pageIndex=int.Parse(txtCurrentPage.Text)-1;
SetPage();
}
3、SQLServer创建存储过程PageTest:
CREATEPROCEDURE[dbo].[PageTest]
@PageSizeINT,
@PageIndexINT,
@TotalCountINTOUTPUT
AS
BEGIN
--总记录数
SELECT@TotalCount=COUNT(1)FROMMF_MO
--记录返回(使用动态SQL绕开参数嗅探问题,效率大幅度提升。)
DECLARE@SQLNVARCHAR(1000)
SET@SQL=
'SELECTTOP('+CONVERT(VARCHAR(32),@PageSize)+')MO_NO,MRP_NO,QTY,BIL_NO'+
'FROMMF_MOA'+
'WHERENOTEXISTS(SELECT1FROM(SELECTTOP('+CONVERT(VARCHAR(32),(@PageIndex-1)*@PageSize)+')MO_NOFROMMF_MOORDERBYMO_NO)BWHEREA.MO_NO=B.MO_NO)'+
'ORDERBYMO_NO'
EXEC(@SQL)
END
4、执行程序:
好了,分享就到此结束了,希望对有此需要的人有一些帮助。
总结
以上所述是小编给大家介绍的DataGridView使用BindingNavigator实现简单分页功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。