首页 > 开发 > 综合 > 正文

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

2024-07-21 02:17:35
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。先在数据库中定义存储过程,轻易实现百万级数据分页://@pagesize:分页大小,pageindex:页号,@pagecount:总页数,@recordcount:记录数create procedure getcustomdatapage @pagesize int, @pageindex int, @pagecount int output, @recordcount int output asdeclare @sql varchar(1000)select @recordcount=count(*) from productsset @pagecount=ceiling(@recordcount*1.0/@pagesize)if @pageindex = 0 or @pagecount<=1set @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 [email protected], set [email protected] where [email protected]";

            string strsql = "update products set [email protected] where [email protected]";

            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'");

            }         }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表