DataGridView使用自定义控件实现简单分页功能(推荐)
程序员文章站
2022-07-06 11:46:54
本例子使用自定义控件方法实现,数据库使用的是sql server,实现过程如下:
1、新建一个自定义控件,命名为:pagecontrol。...
本例子使用自定义控件方法实现,数据库使用的是sql server,实现过程如下:
1、新建一个自定义控件,命名为:pagecontrol。
2、pagecontrol代码如下:
public partial class pagecontrol : usercontrol { //委托及事件 public delegate void bindpage(int pagesize, int pageindex, out int totalcount); public event bindpage bindpageevent; //属性 public int pagesize { get; set; } = 1; //每页显示记录数 public int pageindex { get; set; } //页序号 public int totalcount { get; set; } //总记录数 public int pagecount { get; set; } //总页数 public pagecontrol() { initializecomponent(); //取消下划线 linkfirst.linkbehavior = linkbehavior.neverunderline; linkprev.linkbehavior = linkbehavior.neverunderline; linknext.linkbehavior = linkbehavior.neverunderline; linklast.linkbehavior = linkbehavior.neverunderline; linkgo.linkbehavior = linkbehavior.neverunderline; } /// <summary> /// 设置页 /// </summary> public void setpage() { //总记录数 int totalcount = 0; bindpageevent(pagesize, pageindex + 1, out totalcount); 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> /// <param name="sender"></param> /// <param name="e"></param> private void linkfirst_linkclicked(object sender, linklabellinkclickedeventargs e) { if (e.button == mousebuttons.left) { pageindex = 0; setpage(); } } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkprve_linkclicked(object sender, linklabellinkclickedeventargs e) { if (e.button == mousebuttons.left) { pageindex--; if (pageindex < 0) { pageindex = 0; } setpage(); } } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linknext_linkclicked(object sender, linklabellinkclickedeventargs e) { if (e.button == mousebuttons.left) { pageindex++; if (pageindex > pagecount - 1) { pageindex = pagecount - 1; } setpage(); } } /// <summary> /// 末页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linklast_linkclicked(object sender, linklabellinkclickedeventargs e) { if (e.button == mousebuttons.left) { pageindex = pagecount - 1; setpage(); } } /// <summary> /// 只能按0-9、delete、enter、backspace键 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtsetpage_keypress(object sender, keypresseventargs e) { 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> /// <param name="sender"></param> /// <param name="e"></param> private void linkgo_linkclicked(object sender, linklabellinkclickedeventargs e) { if (e.button == mousebuttons.left) { go(); } } private void go() { 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> /// <param name="sender"></param> /// <param name="e"></param> private void linkfirst_mousemove(object sender, mouseeventargs e) { linkfirst.linkcolor = color.red; } /// <summary> /// linkfirst鼠标离开颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkfirst_mouseleave(object sender, eventargs e) { linkfirst.linkcolor = color.black; } /// <summary> /// linkprev鼠标移过颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkprev_mousemove(object sender, mouseeventargs e) { linkprev.linkcolor = color.red; } /// <summary> /// linkprev鼠标离开颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkprev_mouseleave(object sender, eventargs e) { linkprev.linkcolor = color.black; } /// <summary> /// linknext鼠标移过颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linknext_mousemove(object sender, mouseeventargs e) { linknext.linkcolor = color.red; } /// <summary> /// linknext鼠标离开颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linknext_mouseleave(object sender, eventargs e) { linknext.linkcolor = color.black; } /// <summary> /// linklast鼠标移过颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linklast_mousemove(object sender, mouseeventargs e) { linklast.linkcolor = color.red; } /// <summary> /// linklast鼠标离开颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linklast_mouseleave(object sender, eventargs e) { linklast.linkcolor = color.black; } /// <summary> /// linkgo鼠标移过颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkgo_mousemove(object sender, mouseeventargs e) { linkgo.linkcolor = color.red; } /// <summary> /// linkgo鼠标离开颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkgo_mouseleave(object sender, eventargs e) { linkgo.linkcolor = color.black; } }
3、sql server创建存储过程pagetest:
create procedure [dbo].[pagetest] @pagesize int, @pageindex int, @totalcount int output as begin --总记录数 select @totalcount=count(1) from mf_mo --记录返回(使用动态sql绕开参数嗅探问题,效率大幅度提升。) declare @sql nvarchar(1000) set @sql= 'select top ('+convert(varchar(32),@pagesize)+') mo_no,mrp_no,qty,bil_no '+ 'from mf_mo a '+ 'where not exists (select 1 from (select top ('+convert(varchar(32),(@pageindex-1)*@pagesize)+') mo_no from mf_mo order by mo_no) b where a.mo_no=b.mo_no) '+ 'order by mo_no' exec (@sql) end
4、新建一个winform程序,命名为main,并拖入一个datagridview控件及上面新建的pagecontrol控件,代码如下:
private void main_load(object sender, eventargs e) { pagecontrol1.pagesize = 20; pagecontrol1.pageindex = 0; pagecontrol1.bindpageevent += bindpage; pagecontrol1.setpage(); } /// <summary> /// 绑定页 /// </summary> /// <param name="pagesize">每页显示记录数</param> /// <param name="pageindex">页序号</param> /// <param name="totalcount">总记录数</param> private void bindpage(int pagesize, int pageindex, out int totalcount) { sqlconnection conn = null; sqlcommand cmd = null; totalcount = 0; #region 连接数据库测试 try { //数据库连接 conn = new sqlconnection("server=.;database=db_test;uid=sa;pwd=********;"); conn.open(); //sqlcommand cmd = new sqlcommand(); cmd.connection = conn; cmd.commandtext = "pagetest"; cmd.commandtype = commandtype.storedprocedure; sqlparameter[] param = { new sqlparameter("@pagesize",sqldbtype.int), new sqlparameter("@pageindex",sqldbtype.int), new sqlparameter("@totalcount",sqldbtype.int) }; param[0].value = pagesize; param[1].value = pageindex; param[2].direction = parameterdirection.output; cmd.parameters.addrange(param); //datatable datatable dt = new datatable("mf_mo"); dt.columns.add(new datacolumn("mo_no", typeof(string))); dt.columns.add(new datacolumn("mrp_no", typeof(string))); dt.columns.add(new datacolumn("qty", typeof(decimal))); dt.columns.add(new datacolumn("bil_no", typeof(string))); #region 方法一:sqldatareader sqldatareader dr = cmd.executereader(); dt.load(dr, loadoption.preservechanges); dr.close(); totalcount = (int)param[2].value; datagridview1.datasource = dt; #endregion #region #方法二:sqldataadapter //sqldataadapter da = new sqldataadapter(); //da.selectcommand = cmd; //dt.beginloaddata(); //da.fill(dt); //dt.endloaddata(); //totalcount = (int)param[2].value; //datagridview1.datasource = dt; #endregion } catch (exception ex) { messagebox.show(ex.message, "提示", messageboxbuttons.ok, messageboxicon.information); } finally { conn.close(); cmd.dispose(); } #endregion }
5、执行程序:
总结
以上所述是小编给大家介绍的datagridview使用自定义控件实现简单分页功能,希望对大家有所帮助
上一篇: crontab架构和格式
推荐阅读
-
DataGridView实现简单分页功能
-
DataGridView使用BindingNavigator实现简单分页功能
-
DataGridView使用自定义控件实现简单分页功能(推荐)
-
分页功能的简单实现。使用模板引擎
-
asp.net使用原生控件实现自定义列导出功能的方法
-
DataGridView使用BindingNavigator实现简单分页功能
-
DataGridView实现简单分页功能
-
Android之简单的登录界面的实现、使用 AlertDialog和全局广播实现被强制下线功能、自定义一个带有清除按钮的EditText
-
DataGridView使用自定义控件实现简单分页功能(推荐)
-
分页功能的简单实现。使用模板引擎