首页 > 开发 > 综合 > 正文

(论坛答疑点滴)联合主键的情况怎么在DataGrid中利用DataKeys定位记录?

2024-07-21 02:25:40
字体:
来源:转载
供稿:网友
 

比如表中三个字段
key1 int,key2 int,item varchar(50)
前面2个字段联合主键

前台代码:

<asp:datagrid id="datagrid1" runat="server" autogeneratecolumns="false" datakeyfield="doublekey">
                <columns>
                    <asp:boundcolumn datafield="item" headertext="item"></asp:boundcolumn>
                    <asp:buttoncolumn commandname="del" text="删除"></asp:buttoncolumn>
                </columns>
            </asp:datagrid>


后台代码:

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace csdn2
{
    public class webform20 : system.web.ui.page
    {
        protected system.web.ui.webcontrols.datagrid datagrid1;
    
        private void page_load(object sender, system.eventargs e)
        {
            if(!ispostback)
            {
                setbind();
            }
        }
        private void setbind()
        {
            sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings["conn"]);
            sqldataadapter da=new sqldataadapter("select * from doublekey",conn);
            dataset ds=new dataset();
            da.fill(ds);
            datatable dt=ds.tables[0];
            datacolumn dc=new datacolumn("doublekey",typeof(string),"key1+'#'+key2");
            dt.columns.add(dc);
            this.datagrid1.datasource=dt;
            this.datagrid1.databind();
        }
        web 窗体设计器生成的代码#region web 窗体设计器生成的代码
        override protected void oninit(eventargs e)
        {
            //
            // codegen: 该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
        
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {    
            this.datagrid1.itemcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.datagrid1_itemcommand);
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion

        private void datagrid1_itemcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
        {
            if(e.commandname=="del")
            {
                sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings["conn"]); 
                sqlcommand comm=new sqlcommand("delete from doublekey where [email protected] and [email protected]",conn); 
                sqlparameter parm1=new sqlparameter("@key1",sqldbtype.int);
                sqlparameter parm2=new sqlparameter("@key2",sqldbtype.int); 
                string [] keys=this.datagrid1.datakeys[e.item.itemindex].tostring().split('#');
                parm1.value=keys[0];
                parm2.value=keys[1];
                comm.parameters.add(parm1);
                comm.parameters.add(parm2); 
                conn.open(); 
                comm.executenonquery(); 
                conn.close(); 
                setbind(); 
            }
        }
    }
}



收集最实用的网页特效代码!

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