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

先在数据库中定义存储过程,轻易实现百万级数据分页:
//@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’"); 
            } 
        }
[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:创建完全可编辑的 DataGrid (1)
下一篇:如何给DataGrid添加自动增长列
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • 挑战C#学习的最快速度
  • C#模仿QQ截图功能
  • C# 关于开机自动运行程序方式之一
  • 第一章 C#简介
  • 利用C#实现分布式数据库查询
  • Visual Studio 2005 Hands-On Tutorial - P
  • C#入门代码
  • .NET架构与模式探索
  • 用C#代码编写的SN快速输入工具
  • C# 关于开机自动运行程序方式之一
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • 彻底剖析C# 2.0泛型类的创建和使用
  • 对C# 2.0中匿名方法的怀疑分析
  • EasySP管理解决方案基于Microsoft .NET架构
  • .NET架构与模式探索
  • .NET架构的核心开发技术
  • 用C#代码编写的SN快速输入工具
  • C#链接数据库技巧
  • C#设计模式编程之抽象工厂模式新解
  • 第一章 C#简介
  • 第七章 异常处理
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静