国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

            }         }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广水市| 昭觉县| 凯里市| 鞍山市| 宜黄县| 昆明市| 东山县| 蚌埠市| 新闻| 襄垣县| 横峰县| 中阳县| 资兴市| 宜兰县| 青海省| 余江县| 霍邱县| 慈溪市| 土默特右旗| 沙雅县| 进贤县| 精河县| 马公市| 苏尼特右旗| 巴中市| 怀来县| 邯郸市| 金寨县| 客服| 台湾省| 黔西县| 布尔津县| 天台县| 洛隆县| 渝中区| 贺兰县| 忻州市| 尼勒克县| 白城市| 钦州市| 德庆县|