先在数据库中定义存储过程,轻易实现百万级数据分页:
//@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’"); 
            } 
        }

转自: http://blog.csdn.net/JavaProgramers/archive/2006/07/18/935698.aspx ,,今天一不小心转了几篇文章!但我还是坚持自己写的!

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

  1. Henry手记—Web Form中的Datagrid的自定义分页 (转)

    Henry手记-Web Form中的Datagrid的自定义分页 (转)[@more@]  Henry手记-web Form中的Datagrid的自定义分页XML:namespace prefix = ...

  2. Henry手记—Web Form中的Datagrid的自定义分页

             Henry手记-Web Form中的Datagrid的自定义分页 韩睿  ( 05/31/2003) ASP.NET带给我们很多惊喜,强大的Web Form控件自然是其中的重要部分. ...

  3. Web Form中的Datagrid的自定义分页

    ASP.NET带给我们很多惊喜,强大的Web Form控件自然是其中的重要部分.这其中,最受关注的当然是Datagrid.在ASP中用HTML标记语法来输出数据的方法在Datagrid数据绑定面前显得 ...

  4. Henry手记—Web Form中的Datagrid的自定义分页(转)

    原文:http://blog.csdn.net/Latitude/archive/2003/06/02/17227.aspx 韩睿  ( 05/31/2003) ASP.NET带给我们很多惊喜,强大的 ...

  5. JS-隔行换色+鼠标移上去变色

    <style>ul{list-style: none;} </style> <body><ul><li>111</li>< ...

  6. 用jQuery一句话实现鼠标移上变色

    按钮移上变色效果 <style> .round-corner-btn {             -moz-border-radius:4px;             -webkit-b ...

  7. html鼠标移上去变色放大,CSS3 鼠标滑过图片突出放大效果 | 腾讯云

    今天给大家分享一款简单实用的CSS3鼠标滑过图片放大特效,我们可以将它应用在相册中,或者是轮播展示的图片中,这样可以将鼠标移到图片上进行快速预览图片.同时你也可以在此基础上扩展它,比如给图片加投影和边 ...

  8. ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页

    关键字:DataGrid.存储过程.分页 出自: http://blog.csdn.net/yzx110/archive/2004/08/18/78525.aspx 摘要:在最进的一个项目中因为一个管 ...

  9. ASP.NET中 DataGrid简单自定义分页

    先在pageload中添加事件         private void Page_Load(object sender, System.EventArgs e)         {          ...

最新文章

  1. 核酸序列特征信息分析
  2. 普通话计算机考试相关信息,普通话考试常见问题有哪些
  3. 进度条(python 实现)
  4. oracle 提取首字母,oracle 取字段文字拼音首字母
  5. 清华提出LogME,无需微调就能衡量预训练模型的下游任务表现!
  6. java爬取网页并保存_第九讲:Python爬取网页图片并保存到本地
  7. OpenCV之图像的遮挡与切分、合并(笔记06)
  8. mac 连接hbase的图形化界面_Mac 视觉史(二):90 年代失败 Mac 操作系统大赏
  9. golang 函数一 (定义、参数、返回值)
  10. HMM隐马尔可夫模型(HMM)攻略
  11. C#设计模式---迭代器模式(Iterator Pattern)
  12. Atitit zip解压文件 java use apache ant.jar C:\0wkspc\hislog\src\main\java\com\attilax\compress\ZipUt
  13. 微软ime日文输入法怎么设置切换过来就是输入假名的状态
  14. 数字电视业务PSI-SI学习系列
  15. 暑期参加CSDN编程竞赛的些许心得体会
  16. FastReport for Delphi2010 中文菜单显示不全或者乱码解决方法
  17. 英文歌曲:A place nearby (天堂若比邻)
  18. 【用户画像】Redis的常用五大数据类型和配置文件介绍
  19. 47 lvs-nat/dr
  20. TCP标志位详解(TCP Flag)

热门文章

  1. 基于android的串口开发板,210开发板Android系统串口程序
  2. 华为OD机试季度总结,看一看3月都有哪些华为OD机试问题吧
  3. qq手机管家的一些想法
  4. https 证书安装指引
  5. questmobile2020年app排行榜_2020年 中国手机App用户量排行榜top100
  6. Springboot揭秘-快速构建微服务体系-王福强-2016年5月第一次印刷
  7. 如何制作docker镜像
  8. 告诉你咱穷人怎么过情人节
  9. 前端之样式化链接、web字体
  10. PDAF相位对焦原理