dataGrid添加行的问题

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

datagrid1的readonly为false  
  我想达到这样的目的:  
  单击“添加”按钮,datagrid1在当前行上面插入一行,并且currentcell是第一列,请教各位怎么做。

· 网友精彩回答:

发表者:lovefootball)

先得到当前行  
  然后修改数据源,然后重新绑定  
  然后设置currentcell是第一列

发表者:silverseven7

private   void   btn_misc_insert_click(object   sender,   system.eventargs   e)  
  {  
  try  
  {  
  comlogoutput.writelogfile("btn_misc_insert_click   begin");  
   
  if(this.session["dsp_tripreport_misc"]==null)  
  {  
  binddata_pnlmisc();  
  }  
   
  datatable   dtsource =   (datatable)session["dsp_tripreport_misc"]   ;  
   
  for(   int   i   =   0   ;   i   <   this.dg_misc.items.count   ;   i   ++   )  
  {  
  //   get   update   value  
  textbox   txtcity =   (textbox)dg_misc.items[i].findcontrol("dg_misc_txt_country");  
  dropdownlist   uccurr =   (dropdownlist)dg_misc.items[i].findcontrol("dg_misc_curr");  
  string   tripdate =   ((uc_calendartext)this.dg_misc.items[i].findcontrol("dg_misc_txt_tripdate")).text.trim();  
  string     particular =   ((textbox)this.dg_misc.items[i].findcontrol("dg_misc_txt_particular")).text.trim();  
  string     purpose =   ((textbox)this.dg_misc.items[i].findcontrol("dg_misc_txt_purpose")).text.trim();  
  string     amt =   ((textbox)this.dg_misc.items[i].findcontrol("dg_misc_txt_amt")).text.trim();  
  string     localamt =   ((textbox)this.dg_misc.items[i].findcontrol("dg_misc_txt_localamt")).text.trim();  
  string     receipt =   ((textbox)this.dg_misc.items[i].findcontrol("dg_misc_txt_receipt")).text.trim();  
   
  if(comstylecheck.checkdatestyle(tripdate))  
  {  
  dtsource.rows[i]["trip_date"] =   tripdate   ;  
  }  
   
  if( comstylecheck.checkrequired(amt)   &&   comstylecheck.isdecimail(amt))  
  {  
  dtsource.rows[i]["amt"] =   amt;  
  }  
  if( comstylecheck.checkrequired(localamt)   &&   comstylecheck.isdecimail(localamt))  
  {  
  dtsource.rows[i]["local_amt"] =   localamt;  
  }  
   
  dtsource.rows[i]["particular"] =   particular;  
  dtsource.rows[i]["purpose"] =   purpose;  
  dtsource.rows[i]["attachment"] =   receipt;  
  dtsource.rows[i]["curr"] =   uccurr.selecteditem.value;  
  dtsource.rows[i]["country"] =   txtcity.text;  
  }  
   
  //   add   new   row  
  datarow   dr =   dtsource.newrow();  
  if(dtsource.rows.count>0)  
  {  
  string   lastdate     =   dtsource.rows[dtsource.rows.count-1]["trip_date"].tostring();  
  if(   comstylecheck.checkdatestyle(lastdate))  
  {  
  dr["trip_date"]   =   datetime.parse(lastdate).adddays(1).tostring("dd/mm/yyyy");  
  }  
  else  
  {  
  dr["trip_date"]   =   datetime.parse(gd_start.value).tostring("dd/mm/yyyy");  
  }  
  }  
  else  
  {  
  dr["trip_date"]   =   datetime.parse(gd_start.value).tostring("dd/mm/yyyy");  
  }  
   
  dr["attachment"]   =   "d-";  
  dtsource.rows.add(dr);  
   
  dg_misc.datasource =   dtsource;  
  dg_misc.databind();  
   
  comlogoutput.writelogfile("btn_misc_insert_click   end");  
  }  
  catch(exception   ex)  
  {  
  comlogoutput.writelogfile(ex);  
   
  lbl_err.text   =   ex.message;  
  }  
  }  
   
   
   
   
   
   
  private   void   binddata_pnlmisc()  
  {  
  try  
  {  
  comlogoutput.writelogfile("binddata_pnlmisc   begin");  
   
  //   get   tirp   misc   expenses  
  dataset   dsexc =   new   tripreportdb().gettripmisc(is_serial.value,gs_cmpy.value);  
   
  session["dsp_tripreport_misc"] =   dsexc.tables[0];  
  dg_misc.datasource =   dsexc.tables[0];  
  dg_misc.databind();  
   
  //   calculate   total   misc   amt    
  miscamtchanged(new   object(),new   eventargs());  
   
  if(dsexc.tables[0].rows.count>0)  
  {  
  txt_misc_remark.text =   dsexc.tables[0].rows[0]["remark"].tostring();  
  }  
   
  comlogoutput.writelogfile("binddata_pnlmisc   end");  
   
  }  
  catch(exception   ex)  
  {  
  comlogoutput.writelogfile(ex);  
   
  lbl_err.text   =   ex.message;  
  }  
  }

发表者:singlepine

http://dotnet.aspx.cc/showdetail.aspx?id=c4ce487d-02fb-450b-bf3c-9f870cbc66a5

发表者:juge001

当点了添加的时候把邦定datagrid的ds的第一行加入一新行,再进行邦定.  
  把currentcell设为第一列  
  当当前行为第一行的时候readonly为false;不然为true  
  不知道这是不是楼主想要的结果

发表者:zdjray

这个有点麻烦了,为什么要加在上面?

发表者:songsu

//------------------------插入数据  
  dataset的的结构与数据库相似,因此 插入数据实质上就是在dataset的数据表中添加一条新的数据行(datarow)  
   
  //------------------------新建一个数据行  
  datarow   dr   =   ds.tables["score"].newrow();  
  dr["name"]   =   "比尔";  
  dr["class"]       =   100;                               //[""]引号里面的是数据表中的字段,例如"姓名"   等等  
  dr["chinese"]   =   70;  
  dr["math"]         =   90;  
  dr["english"]   =   90;  
  dr["physics"]   =   90;  
  dr["history"]   =   60;  
  //将新建的数据行加入到datatable的datarow集合中去  
  ds.tables["score"].rows.add(dr);  
   
  //------------------------更新数据库  
  dataadapter.update(dataset);    
  在使用update时,最好还要上一个string类参数,以指定dataset需要要更新的数据表名  
  dataadapter.update(dataset,tablename);

发表者:zhzuo

直接操作数据源,比如你绑定到了dataview   ,或datatable   ,  
  直接在这里添加就行。

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved