♣
dataGrid添加行的问题
datagrid1的readonly为false
我想达到这样的目的:
单击“添加”按钮,datagrid1在当前行上面插入一行,并且currentcell是第一列,请教各位怎么做。
· 网友精彩回答:
先得到当前行
然后修改数据源,然后重新绑定
然后设置currentcell是第一列
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;
}
}
http://dotnet.aspx.cc/showdetail.aspx?id=c4ce487d-02fb-450b-bf3c-9f870cbc66a5
当点了添加的时候把邦定datagrid的ds的第一行加入一新行,再进行邦定.
把currentcell设为第一列
当当前行为第一行的时候readonly为false;不然为true
不知道这是不是楼主想要的结果
这个有点麻烦了,为什么要加在上面?
//------------------------插入数据
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);
直接操作数据源,比如你绑定到了dataview ,或datatable ,
直接在这里添加就行。
- 更多问题:
- · 怎么在DataGrid中动态改变CheckBox的选中状态?
- · 内存DC保存CBrush的问题
- · 想好好研究一下c标准库,哪位仁兄可介绍本好书
- · ------------JS空件birthday.js 显示年月空件的一个疑问!-----------
- · 求一SQL语句!
- · 问身边牛人没有解决的关于进程间通信的问题
- · 一个SQL问题
- · 在C#里怎么读去EXCEL的单元格里的数据,
- · null,false,true与NULL,FALSE,TRUE的区别
- · 奇怪的jsp+tomcat5.0.18中文乱码问题!!!
- · 学了asp,asp.net,jsp;做了网站,OA;现在一个小地方混,工资,待遇,前景比较迷茫
- · 请问一下这种xml文件怎么来读取啊
- · 给你一个网址,然后怎么样把这个网址里的所有内容打印出来?
- · asp.net程序出错问题
- · 请教关于CPtrArray的问题
- · servlet 中的Dll调用问题 ,多谢了!

