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

displaytag分页和鼠标经过变色功能 displaytag标签扩展:鼠标经过行变色功能 立即下载
 
先在数据库中定义存储过程,轻易实现百万级数据分页:
//@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’"); 
            } 
        }
LTspice基础教程-028.Timestep too small Q: “Timestep too small” error message A: If the LTspice program can’t find a solution for the guessed time step, then the program decreases the time step and tries again. This can’t work forever. If... 阅读详情

相关推荐

win10 运行lisflood_8踩过的坑

1、安装cuda及VS2019 https://www.bilibili.com/read/cv7335974/ 2、netcdf.h头文件找不到问题 https://www.cnblogs.com/lyggqm/p/12926974.html https://blog.csdn.net/u011954296/article/details/54617048 3、编码问题 https://blog.csdn.net/qq_33485434/article/details/81196843 4、

我的笔记 4121

(ASP.NET)修改和删除DataGrid行——数据库访问

本程序涉及到数据库的添加,修改和删除操作。 懒得写了,把界面贴出来,照着界面画就可以了。本例数据库:sqlserver2000附带的pubs数据库,看一下连接字符串就很清楚了。如果要在本机器上运行,把uid和pwd改成你自己sql登陆用户名和密码。  创建一个web页面,命名为:add.aspx。 界面设计如图:  add.aspx代码: &lt;%@ page language="c#" cod...

water84222的专栏 442

解决2020R2,2022R1等版本的SCDM无法编辑草图尺寸的问题

解决2020R2,2022R1等版本的SCDM无法编辑草图尺寸的问题

y_u_yu_yu_的博客 1万+

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

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

fzzf 894

SPSS回归分析(线性:一元(简单)和多重;非线性logistic)

SPSS线性回归分析回归分析一、线性模型(针对连续因变量,线性关系)(一)简单线性回归(一元线性回归)1、绘制散点图2、建立简单(一元)线性回归模型3、建立模型后,检查预测结果(二)多重回归分析1、绘制矩阵分布散点图(论文专用你懂的)2、建立多重回归模型point 1:自变量加入方法:逐步(步进)法3、建立模型后,检查预测结果二、非线性模型(将非线性问题转换为线性问题)1、logistic分析的回归模型1.1 建立回归模型1.2 利用回归模型进行预测 根据之前的学习,通过评价实验得到了主观数据, 通过 描述

weixin_44394352的博客 5万+

Petalinux实战:3种方法调试AXI UART Lite设备(含minicom/screen/直接寄存器操作对比)

本文详细介绍了在Zynq平台上使用Petalinux调试AXI UART Lite设备的三种实用方法:用户空间工具(minicom/screen)、应用层程序(termios配置)和底层寄存器直接操作。文章对比了各方法的优缺点与适用场景,并重点解析了设备树配置、驱动加载等关键环节,帮助开发者快速定位并解决串口通信中的常见问题。

weixin_29190169的博客 436
上一篇: asp.net 中DataGrid自定义分页(简单,实用,易懂)
下一篇: (C#)DataGrid实现自定义分页,鼠标移至变色,删除确认、可编辑,可删除
JavaProgramers
博客等级 码龄22年 660粉丝 304原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值