(C#)DataGrid实现自定义分页,鼠标移至变色,删除确认、可编辑,可删除

漂亮的datagridviewC#皮肤 漂亮的datagridview C#皮肤很不错的源代码 立即下载

先在数据库中定义存储过程,轻易实现百万级数据分页:

//@PageSize:分页大小,PageIndex:页号,@PageCount:总页数,@recordCount:记录数

CREATE PROCEDURE GetCustomDataPage @pageSize int, @pageIndex int, @pageCount int output, @recordCount int output AS

declare @SQL varchar(1000)

select @recordCount=count(*) from products

set @pageCount=ceiling(@recordCount*1.0/@pageSize)

if @pageIndex = 0 or @pageCount<=1

 set @SQL='select top '+str(@pageSize)+' productID,productName, unitPrice from products order by productID asc'

else if @pageIndex = @pageCount -1

 set @SQL='select * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' productID,productName, unitPrice from products order by productID desc) TempTable order by productID asc'

else  set @SQL='select top '+str(@pageSize) +' * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' productID,productName, unitPrice from products order by productID desc) TempTable order by productID asc'

exec(@SQL)

GO

好了,存储过程建好了,那么如何在.Net中使用呢?请看以下代码:

        private uint pageCount;  //总页数

        private uint recordCount;  //总记录数

 

        private DataSet GetPageData(uint pageSize, uint pageIndex)

        {

            string strConn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];           

            SqlConnection conn = new SqlConnection(strConn);

 

            conn.Open();

 

            SqlCommand command = new SqlCommand("GetCustomDataPage",conn);  //第一个参数为存储过程名

            command.CommandType = CommandType.StoredProcedure;   //声明命令类型为存储过程

 

            command.Parameters.Add("@pageSize",SqlDbType.Int);

            command.Parameters["@pageSize"].Value = pageSize;

 

            command.Parameters.Add("@pageIndex",SqlDbType.Int);

            command.Parameters["@pageIndex"].Value = pageIndex;

 

            command.Parameters.Add("@pageCount",SqlDbType.Int);

            command.Parameters["@pageCount"].Value = pageCount;

            command.Parameters["@pageCount"].Direction = ParameterDirection.Output;  //存储过程中的输出参数

 

            command.Parameters.Add("@recordCount",SqlDbType.Int);

            command.Parameters["@recordCount"].Value = recordCount;

            command.Parameters["@recordCount"].Direction = ParameterDirection.Output; //存储过程中的输出参数

 

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataSet ds = new DataSet();

            adapter.Fill(ds);           

 

            //获得输出参数值

            pageCount = Convert.ToUInt32(command.Parameters["@pageCount"].Value);

            recordCount = Convert.ToUInt32(command.Parameters["@recordCount"].Value);

           

            conn.Close();

            return ds;

        }

 

        //绑定数据到DataGrid中

        private void BindDataGrid()

        {

            DataSet ds = GetPageData((uint)dgProduct.PageSize,(uint)dgProduct.CurrentPageIndex);

            dgProduct.VirtualItemCount = (int)recordCount;

            dgProduct.DataSource = ds;

            dgProduct.DataBind();

        }

 

        //页面加载时就绑定DataGrid

        private void Page_Load(object sender, System.EventArgs e)

        {

            if(!Page.IsPostBack)

            {

               BindDataGrid();

            }

        }

        //用户翻页时事件处理

        private void dgProduct_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

        {

            dgProduct.CurrentPageIndex = e.NewPageIndex;

            BindDataGrid();

        }

 

        //用户单击编辑按纽时事件处理

        private void dgProduct_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

        {

            dgProduct.EditItemIndex = e.Item.ItemIndex;

            BindDataGrid();

        }

 

        //用户单击取消按纽时事件处理

        private void dgProduct_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

        {

            dgProduct.EditItemIndex = -1;

            BindDataGrid();

        }

 

        //用户单击更新按纽时事件处理

        private void dgProduct_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

        {

            string strConn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];           

            SqlConnection conn = new SqlConnection(strConn);

 

            conn.Open();

            //string strSQL = "update from products set productName=@productName, set unitPrice=@unitPrice where productID=@productID";

            string strSQL = "update products set productName=@productName where productID=@productID";

            SqlCommand command = new SqlCommand(strSQL,conn);

 

            command.Parameters.Add("@productName",SqlDbType.NVarChar,40);

            command.Parameters["@productName"].Value = ((TextBox)(e.Item.Cells[1].Controls[0])).Text.Trim();

 

            //command.Parameters.Add("@unitPrice",SqlDbType.Int);

            //command.Parameters["@unitPrice"].Value = Convert.ToInt32(((TextBox)(e.Item.Cells[2].Controls[0])).Text.Trim());

 

            command.Parameters.Add("@productID",SqlDbType.Int);

            command.Parameters["@productID"].Value = dgProduct.DataKeys[e.Item.ItemIndex];

 

            command.ExecuteNonQuery();

            conn.Close();

 

            dgProduct.EditItemIndex = -1;

            BindDataGrid();

        }

 

       //用户单击删除按纽时事件处理

        private void dgProduct_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

        {

            string strConn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

            SqlConnection conn = new SqlConnection(strConn);

 

            conn.Open();

            SqlCommand command = new SqlCommand("DeleteProduct",conn);

            command.CommandType = CommandType.StoredProcedure;

           

            command.Parameters.Add("@productID",SqlDbType.Int);

            command.Parameters["@productID"].Value = dgProduct.DataKeys[e.Item.ItemIndex];

 

            command.ExecuteNonQuery();

            BindDataGrid();

        }

 

        //实现删除确认及颜色交替显示功能

        private void dgProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

        {

            if(e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)

            {

               Button btnDelete = (Button)(e.Item.Cells[4].Controls[0]);

               btnDelete.Attributes.Add("onClick","JavaScript:return confirm('确定删除?')");

 

               e.Item.Attributes.Add("onMouseOver","this.style.backgroundColor='#FFCC66'");

               e.Item.Attributes.Add("onMouseOut","this.style.backgroundColor='#ffffff'");

            }

        }


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C#DataGridView分页显示功能实现 DataGridView分页显示功能的实现,文件的只需修改命名空间便可以移植到其他WinForm项目中。 立即下载

相关推荐

BRepNet与图神经网络融合:提升3D CAD加工特征识别的精度与泛化能力

本文探讨了BRepNet与图神经网络(GNN)融合技术如何显著提升3D CAD加工特征识别的精度与泛化能力。通过将CAD模型视为图结构,利用GNN的消息传递机制增强对复杂拓扑关系的理解,并结合BRepNet的局部特征提取,该融合方法在MFCAD++数据集上实现了高达99.53%的准确率,有效解决了传统方法在处理复杂相交特征时的局限性,为智能制造提供了关键技术支撑。

android的专栏 781

DataGridView翻页(WinForm)

思想: 将DataGridView界面设置成显示固定条数的大小,比如要显示10条数据,就将DataGridView设成只能显示10条数据的大小。根据设置FirstDisplayedScrollingRowIndex属性,实现翻页效果。(实际上,这种实现方式只是将数据隐藏了,翻页时将要显示的数据放到第一行的位置,让人看起来就像是翻页了……) FirstDisplayedScrollingRo...

weixin_30525825的博客 1713

oracle两节点RAC,由于gipc导致某节点crs无法启动问题分析

两节点RAC,其中1 节点集群CRS无法启动。经过分析原因为2节点gipcd进程异常,导致节点之间无法正常通信,重启2节点gipcd.bin后问题得以恢复。 从现象来看,是 ora.crsd...

cuibaye0692的博客 4539

DataGrid实现自定义分页,鼠标移至变色,删除确认、可编辑,可删除

数据库:SqlServer 2000, 数据源:系统自带的Northwind操作系统:Window XP 中文版开发平台:.Net Framework 1.1, C#语言效果预览:   好了,让我们开始奇异的Datagrid之旅吧: 先在数据库中定义存储过程,轻易实现百万级数据分页://@PageSize:分页大小,PageIndex:页号,@PageCount:总页数

jxufewbt的专栏 1131

Ubuntu20.04 高效安装企业微信的完整指南

本文提供了在Ubuntu20.04系统上高效安装企业微信的完整指南,详细介绍了通过Deepin-wine环境运行Windows版企业微信的步骤和优化技巧。内容包括系统环境检查、依赖安装、企业微信包的获取与安装、首次运行配置及常见问题解决方案,帮助开发者在Ubuntu系统中流畅使用企业微信进行办公通讯。

weixin_42568801的博客 358

别再乱短接了!OEC/OEC-turbo刷机救砖,用RKDevTool避开‘下载boot失败’的保姆级教程

本文提供OEC/OEC-turbo刷机救砖的终极指南,重点介绍使用RKDevTool避开‘下载boot失败’的详细步骤。从Loader模式的优势到硬件准备细节,再到分阶段刷机方案,帮助用户高效完成设备恢复,避免反复短接和常见误区。

weixin_29280657的博客 1140

vs.net beta 2中利用DataGrid分页详解

beta 2中利用DataGrid分页 说明: 1、本篇文章只适用于初学vs.net者,尤其对vs.net环境还不是很熟悉者。 2、需要的环境:win2000, sql server2000, vs.net beta2 步

carper的专栏 3391

MobaXterm破解14个会话限制的保姆级教程(附Python环境配置指南)

本文提供了MobaXterm破解14个会话限制的详细教程,并深入讲解了如何配置稳定的Python环境以支持相关脚本运行。通过理解软件授权机制、安全实践步骤以及许可文件的应用,帮助用户解除会话数量限制,实现高效的远程服务器管理与自动化运维。

weixin_29211309的博客 1733

easyuI中datagrid控件讲解

datagrid控件:在VisualStudio中Razor视图引擎中使用datagrid控件实现对应的效果

weixin_41563376的博客 2672

海豚调度器(dolphinscheduler)再次使用,添加钉钉预警

1,首先我们部署是海豚调度器 1.3.8版本,由于页面只有邮件预警,所以在了解源码的同时,需要添加钉钉预警 2,说明,这一块是同事搞的,不是我搞的,新版本的dolphinscheduler已经有了钉钉模块,只是新版本还没有发布 3,我们现在只是添加简单的钉钉预警,任务成功或者失败会发送邮件+钉钉 4,具体操作 找到最新的源码devhttps://github1s.com/apache/dolphinscheduler/blob/dev/dolphinscheduler-alert/src/ma...

qq_31866793的博客 3086

RML2016.10a (CNN、LSTM、CNN-LSTM、CNN-LSTM-Multi-head-attention, TensorFlow)

RML2016.10a (CNN、LSTM、CNN-LSTM、CNN-LSTM-Multi-head-attention, TensorFlow) 包含所有数据和代码,支持一键运行(jupyter)

qq_40999533的博客 1492

VB.NET 给DataGrid加上右键菜单

代码如下: Dim fileName As String Dim MyXmlDataSet As New DataSet fileName = "sunhai.xml" MyXmlDataSet.ReadXml(fileName) Form1.DefInstance.DataGrid1.DataSource = MyXmlDataSet   启动程序,DataTable默认是闭合的,

1981

UDS19 03 和19 04服务--答疑

UDS 1903服务(reportDTCSnapshotIdentification)用于获取ECU中存储的故障码快照标识信息。该服务返回DTC编号及对应的快照记录编号,为后续使用1904服务读取详细快照数据提供依据。请求报文为"1903",响应报文格式为"5903"后接DTC编号和记录编号列表。该服务需在非默认会话模式下执行,且清除诊断信息会删除相关快照记录。1903服务是读取DTC快照数据的基础步骤,可帮助确定ECU中存储的故障快照情况。

汽车软件开发 475
上一篇: DataGrid实现自定义分页,鼠标移至变色,删除确认、可编辑,可删除
下一篇: 值得一看的Struts入门教程
JavaProgramers
博客等级 码龄22年 660粉丝 304原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值