一次单击添加空白记录(one click add blank record)

   IT问题网   2020-06-29 00:00:00

问 题

亲爱的先生,

当点击编辑链接按钮时,还有一个空白记录添加到网格中请更正我。



代码是:

  private   void  binddata()
{
string selectsql = " select * from reservation";
ocon.open();
sqlcommand cmd = new sqlcommand(selectsql,ocon);
sqldataadapter adapter = new sqldataadapter(cmd);
dataset ds = new dataset();

adapter.fill(ds," reservation");
ocon.close();
gvreservation.datasource = ds;
gvreservation.databind();
}

受保护 void gvreservation_rowcommand( object sender,gridviewcommandeventargs e)
{
string id =((textbox)gvreservation.footerrow.findcontrol (" txtfcustomerid"))。文字;
string name =((textbox)gvreservation.footerrow.findcontrol(" txtfcustomername"))文本;
string age =((textbox)gvreservation.footerrow.findcontrol(" txtfage"))文本;
string location =((dropdownlist)gvreservation.footerrow.findcontrol(" ddlflocation"))的文本。
string class1 =((dropdownlist)gvreservation.footerrow.findcontrol(" ddlfclass"))文本;
ocon.open();
string sqlstatement = " 插入预订(客户id,客户名称,年龄,位置,班)的值(@客户id,@客户名称,@年龄,@位置,@类)";
sqlcommand cmd = new sqlcommand(sqlstatement,ocon);
cmd.parameters.add(" @ customerid",id);
cmd.parameters.add(" @ customername",name);
cmd.parameters.add(" @ age",年龄);
cmd.parameters.add(" @ location",location);
cmd.parameters.add(" @ class",class1);
cmd.executenonquery();
ocon.close();
binddata();
}

受保护 void gvreservation_rowediting( object sender,gridviewediteventargs e)
{
string id =((textbox)gvreservation.footerrow.findcontrol (" txtfcustomerid"))。文字;
string name =((textbox)gvreservation.footerrow.findcontrol(" txtfcustomername"))文本;
string age =((textbox)gvreservation.footerrow.findcontrol(" txtfage"))文本;
string location =((dropdownlist)gvreservation.footerrow.findcontrol(" ddlflocation"))的文本。
string class1 =((dropdownlist)gvreservation.footerrow.findcontrol(" ddlfclass"))文本;
string sqlstatements = " 更新预订集customername ='" + name + " ',age ='" + age + " ',location ='" + location + " ',class ='" + class1 + " 'where customerid =' " + id + " '";
ocon.open();
sqlcommand cmd = new sqlcommand(sqlstatements,ocon);
cmd.parameters.add(" @ customerid",id);
cmd.parameters.add(" @ customername",name);
cmd.parameters.add(" @ age",年龄);
cmd.parameters.add(" @ location",location);
cmd.parameters.add(" @ class",class1);
cmd.executenonquery();
ocon.close();
binddata();
}

受保护 void gvreservation_rowdeleting( object sender,gridviewdeleteeventargs e)
{
string id =((textbox)gvreservation.footerrow.findcontrol (" txtfcustomerid"))。文字;
string sqlstatement = " 从预订中删除客户id = @客户id";
ocon.open();
sqlcommand cmd = new sqlcommand(sqlstatement,ocon);
cmd.parameters.add(" @ customerid",id);
cmd.executenonquery();
ocon.close();
binddata();
}
}

解决方案

首先你需要给出命令名称到编辑链接按钮例如。


lt; asp:linkbutton id = "  lnkedit" runat = "  server" commandname = " 编辑" commandargument = " lt;%#container.dataitemindex%gt;" xmlns:asp = " #unknown" gt;  


此链接按钮具有commandname ="编辑"




  protected   void  gvreservation_rowupdating( object  sender,gridv iewupdateeventargs e)
{
string id =((textbox)gvreservation.footerrow.findcontrol(" txtfcustomerid"))。文字;
string name =((textbox)gvreservation.footerrow.findcontrol(" txtfcustomername"))文本;
string age =((textbox)gvreservation.footerrow.findcontrol(" txtfage"))文本;
string location =((dropdownlist)gvreservation.footerrow.findcontrol(" ddlflocation"))的文本。
string class1 =((dropdownlist)gvreservation.footerrow.findcontrol(" ddlfclass"))文本;
string sqlstatements = " 更新预订集customername ='" + name + " ',age ='" + age + " ',location ='" + location + " ',class ='" + class1 + " 'where customerid =' " + id + " '";
ocon.open();
sqlcommand cmd = new sqlcommand(sqlstatements,ocon);
cmd.parameters.add(" @ customerid",id);
cmd.parameters.add(" @ customername",name);
cmd.parameters.add(" @ age",年龄);
cmd.parameters.add(" @ location",location);
cmd.parameters.add(" @ class",class1);
cmd.executenonquery();
ocon.close();
gvreservation.editindex = e.neweditindex;
binddata();

}

标签:一次单击添加空白记录



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

关于sql'列表的问题(the problem which is about listing of sql')

problem i want to get 3 datas which has been added lastly ...

DataGridView绑定源datacontext.ExecuteQuery allowusertoaddrows已禁用(DataGridView Binding Source datacontext.ExecuteQuery allowusertoaddrows disabled)

problem using the following code: iqueryable data = db.e ...

我想在每个页面上显示用户名(I want to display username on every page)

problem can i use loginview control without login control ...

解释如何重载.ToString()方法......(Explain how to overload .ToString() method...)

problem help me friends... :) thanks in advance... soluti ...

UserControl编辑对象(UserControl to edit an object)

problem hello, i have a usercontrol that has some fields ...

使用任何网络服务获取英国的邮政编码和地址(get postal code and address of UK using any web service)

problem hi, i have 2 text box. 1 for u.k. p ...

Windows 7的应用程序(application for windows 7)

problem do i need to install anything to program an app u ...

将控件添加到datagridview(add control to datagridview)

problem hi, there is a control called nbtext (which forma ...

多列和网格下拉列表(无自动完成)(dropdown with multicolumn and grid(no autocomplete))

problem i need a dropdown with multicolumn like the follo ...

如何创建oracle数据库DSN?(how to create oracle database DSN)

problem respected sir/mam i have a oracle database in my ...

获取dataTable中字符串的长度(get length of a string in dataTable)

problem hiii all i make a for loop on datatab ...

使用大型数据库(working with large databases)

problem hi, we use sql server 2008 database ...

数据列表中的CountDown计时器(CountDown timers in a datalist)

problem im developing a auction website where students fr ...

插入记录器的问题(problem in inserting recorod)

problem cmd = new sqlcommand("insert into login1 values" ...

显示行到列(Display Row to Column)

problem how row to column value display in crystal report ...

Gridview中的复选框(Check box in Gridview)

problem if i check checkbox in gridview after that if i c ...

更新DatagridView记录(Updating DatagridView Records)

problem what am i doing wrong i can seem to u ...

如何从tortoiseSVN获取最新版本,然后使用CruiseControl.NET在msbuild中构建它(how to get latest version from tortoiseSVN and then build it in msbuild with CruiseControl.NET)

problem hi, i had a one asp.net solution in ...

请再帮忙,善良的人(Please help again, kind people)

problem hi all, so frustrated. i did two days'' researc ...

如何使用c#,net动态地在textarea中添加文本?(how to add text in textarea dynamically using c#,net)

problem string query = string.empty; query = "select mess ...