当前位置:首页 > 技术问答集
firefox

在web控件datagrid里面怎么样才能对表格进行编辑。加入文本框textbox控件如何加入!

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

如题目所问。

· 网友精彩回答:

发表者:zxm008

不知下段代码对你是否有帮助,编辑时datagrid会自动添加textbox控件  
   
  <%@   import   namespace="system.data.oledb"   %>  
  <script   language="vb"   runat=server>  
   
  定义全局变量myconnection  
  dim   myconnection   as   oledbconnection  
  dim   mycommand   as   oledbcommand  
   
  sub   page_load(sender   as   object,   e   as   eventargs)  
  dim   strcnn   as   string="provider=microsoft.jet.oledb.4.0;   "&   _  
                                            "data   source=e:/source/db/学生.mdb"  
        myconnection=new   oledbconnection(strcnn)  
        myconnection.open  
   
      if(not   page.ispostback)  
                  call   bindgrid  
      end   if  
  end   sub  
   
  sub   bindgrid()  
        dim   strsql   as   string="select   *   from   基本情况"  
        mycommand=new   oledbcommand(strsql,myconnection)  
        dim   mydatareader   as   oledbdatareader  
        mydatareader=mycommand.executereader()  
        mydatagrid.datasource=mydatareader  
        mydatagrid.databind  
  end   sub  
   
  private   sub   datagrid_edit(sender   as   object,e   as   datagridcommandeventargs)  
   
        mydatagrid.edititemindex=e.item.itemindex  
        call   bindgrid  
   
  end   sub  
   
  private   sub   datagrid_cancel(sender   as   object,e   as   datagridcommandeventargs)  
   
        mydatagrid.edititemindex   =   -1  
        call   bindgrid  
  end   sub  
   
  private   sub   datagrid_update(sender   as   object,e   as   datagridcommandeventargs)  
        dim   id,语文,数学,英语   as   string  
        dim   strsql   as   string  
        dim   temptextbox   as   textbox  
         
        id=e.item.cells(1).text  
        temptextbox=e.item.cells(3).controls(0)  
        语文=temptextbox.text  
        temptextbox=e.item.cells(4).controls(0)  
        数学=temptextbox.text  
        temptextbox=e.item.cells(5).controls(0)  
        英语=temptextbox.text  
          strsql="update   基本情况   set   chinese="   &   语文   &   ",math="   &   数学   &   ",english="   &   英语   &   "   where   id="   &   id   &   ""  
  dim   mycommand   as   oledbcommand=new   oledbcommand  
  mycommand.connection=myconnection  
        mycommand.commandtext=strsql  
        mycommand.executenonquery()  
         
        mydatagrid.edititemindex   =   -1  
        call   bindgrid  
   
  end   sub  
  </script>    
   
  <html>  
  <title>演示editcommandcolumn</title>  
  </head>  
  <body>  
  <form   runat=server>  
  <center>  
  <p>演示editcommandcolumn</p>  
          <asp:datagrid   id="mydatagrid"   runat="server"  
    autogeneratecolumns="false"  
    oneditcommand="datagrid_edit"  
    oncancelcommand="datagrid_cancel"    
    onupdatecommand="datagrid_update"   >  
  <columns>  
      <asp:editcommandcolumn   headertext=""   buttontype="pushbutton"  
                edittext="编辑"  
                updatetext="更新"  
                canceltext="取消"   />  
      <asp:boundcolumn     headertext="学号"   datafield="id"   readonly="true"   />  
      <asp:boundcolumn     headertext="姓名"   datafield="name"   readonly="true"   />  
      <asp:boundcolumn     headertext="语文成绩"   datafield="chinese"   />  
      <asp:boundcolumn     headertext="数学成绩"   datafield="math"   />  
      <asp:boundcolumn     headertext="英语成绩"   datafield="english"   />    
   
  </columns>    
  </asp:datagrid>  
  </center>  
  </form>  
  </body>  
  </html>

发表者:breakerfish

其实这个问题也很好办,在datagrid里有,editcommand行为,指要你在它的里面指定datagrid进入编辑模式就行了,如:  
  private   void   edit_event(object   source,   system.web.ui.webcontrols.datagridcommandeventargs   e)  
  {  
  yourdatagrid.edititemindex   =   e.item.itemindex;  
  yourdatagriddatabind();  
  }  
  然后private   void   initializecomponent()  
  {  
   
  this.dgnavigation.editcommand   +=   new   system.web.ui.webcontrols.datagridcommandeventhandler(this.edit_event);  
  this.load   +=   new   system.eventhandler(this.page_load);  
   
  }  
  挂勾上,你就可以编辑了,library上说得很清楚你可以查查~

.
© 2006-2008 All Rights Reserved