怎么在DataGridView中使用自定义控件实现一个分页功能

今天就跟大家聊聊有关怎么在DataGridView中使用自定义控件实现一个分页功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

实现过程如下:

1、新建一个自定义控件,命名为:PageControl。

怎么在DataGridView中使用自定义控件实现一个分页功能

2、PageControl代码如下:

publicpartialclassPageControl:UserControl
{
//委托及事件
publicdelegatevoidBindPage(intpageSize,intpageIndex,outinttotalCount);
publiceventBindPageBindPageEvent;
//属性
publicintPageSize{get;set;}=1;//每页显示记录数
publicintPageIndex{get;set;}//页序号
publicintTotalCount{get;set;}//总记录数
publicintPageCount{get;set;}//总页数
publicPageControl()
{
InitializeComponent();
//取消下划线
linkFirst.LinkBehavior=LinkBehavior.NeverUnderline;
linkPrev.LinkBehavior=LinkBehavior.NeverUnderline;
linkNext.LinkBehavior=LinkBehavior.NeverUnderline;
linkLast.LinkBehavior=LinkBehavior.NeverUnderline;
linkGo.LinkBehavior=LinkBehavior.NeverUnderline;
}
///<summary>
///设置页
///</summary>
publicvoidSetPage()
{
//总记录数
inttotalCount=0;
BindPageEvent(PageSize,PageIndex+1,outtotalCount);
TotalCount=totalCount;
//总页数
if(TotalCount%PageSize==0)
PageCount=TotalCount/PageSize;
else
PageCount=TotalCount/PageSize+1;
//当前页及总页数
txtCurrentPage.Text=(PageIndex+1).ToString();
lblTotalPage.Text="共"+PageCount.ToString()+"页";
}
///<summary>
///首页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkFirst_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse)
{
if(e.Button==MouseButtons.Left)
{
PageIndex=0;
SetPage();
}
}
///<summary>
///上一页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkPrve_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse)
{
if(e.Button==MouseButtons.Left)
{
PageIndex--;
if(PageIndex<0)
{
PageIndex=0;
}
SetPage();
}
}
///<summary>
///下一页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkNext_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse)
{
if(e.Button==MouseButtons.Left)
{
PageIndex++;
if(PageIndex>PageCount-1)
{
PageIndex=PageCount-1;
}
SetPage();
}
}
///<summary>
///末页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkLast_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse)
{
if(e.Button==MouseButtons.Left)
{
PageIndex=PageCount-1;
SetPage();
}
}
///<summary>
///只能按0-9、Delete、Enter、Backspace键
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidtxtSetPage_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;
}
}
///<summary>
///指定页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkGo_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse)
{
if(e.Button==MouseButtons.Left)
{
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();
}
///<summary>
///linkFirst鼠标移过颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkFirst_MouseMove(objectsender,MouseEventArgse)
{
linkFirst.LinkColor=Color.Red;
}
///<summary>
///linkFirst鼠标离开颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkFirst_MouseLeave(objectsender,EventArgse)
{
linkFirst.LinkColor=Color.Black;
}
///<summary>
///linkPrev鼠标移过颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkPrev_MouseMove(objectsender,MouseEventArgse)
{
linkPrev.LinkColor=Color.Red;
}
///<summary>
///linkPrev鼠标离开颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkPrev_MouseLeave(objectsender,EventArgse)
{
linkPrev.LinkColor=Color.Black;
}
///<summary>
///linkNext鼠标移过颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkNext_MouseMove(objectsender,MouseEventArgse)
{
linkNext.LinkColor=Color.Red;
}
///<summary>
///linkNext鼠标离开颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkNext_MouseLeave(objectsender,EventArgse)
{
linkNext.LinkColor=Color.Black;
}
///<summary>
///linkLast鼠标移过颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkLast_MouseMove(objectsender,MouseEventArgse)
{
linkLast.LinkColor=Color.Red;
}
///<summary>
///linkLast鼠标离开颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkLast_MouseLeave(objectsender,EventArgse)
{
linkLast.LinkColor=Color.Black;
}
///<summary>
///linkGo鼠标移过颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkGo_MouseMove(objectsender,MouseEventArgse)
{
linkGo.LinkColor=Color.Red;
}
///<summary>
///linkGo鼠标离开颜色
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidlinkGo_MouseLeave(objectsender,EventArgse)
{
linkGo.LinkColor=Color.Black;
}
}

3、SQL Server创建存储过程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、新建一个WinForm程序,命名为Main,并拖入一个DataGridView控件及上面新建的PageControl控件,代码如下:

privatevoidMain_Load(objectsender,EventArgse)
{
pageControl1.PageSize=20;
pageControl1.PageIndex=0;
pageControl1.BindPageEvent+=BindPage;
pageControl1.SetPage();
}
///<summary>
///绑定页
///</summary>
///<paramname="pageSize">每页显示记录数</param>
///<paramname="pageIndex">页序号</param>
///<paramname="totalCount">总记录数</param>
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
}

看完上述内容,你们对怎么在DataGridView中使用自定义控件实现一个分页功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注恰卡编程网行业资讯频道,感谢大家的支持。

发布于 2021-03-24 01:21:59
收藏
分享
海报
0 条评论
163
上一篇:使用Maven怎么开发一个Web应用 下一篇:怎么在python中对元素进行长截图
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码