第十部分网格视图增加一行
在网格视图的对应行中直接编辑好了之后:
//设置SQL的插入语句
sqlcommand.CommandText =
”INSERT studeng( No ,Name ,Sex ,Class ,Birthdate ,Special) VALUES(@No,@Name,@Sex,@Class,@Birthdate,@Special);”;
//为插入语句中的参数定义赋值,赋值为:网格视图的对应列的值
sqlcommand.Parameters.Add("@No", SqlDbType.Char, 15, "No");
sqlcommand.Parameters.Add("@Name", SqlDbType.VarChar, 30, "Name");
sqlcommand.Parameters.Add("@Sex", SqlDbType.Char, 2, "Sex");
sqlcommand.Parameters.Add("@Class", SqlDbType.VarChar, 30, "Class");
sqlcommand.Parameters.Add("@Birthdate", SqlDbType.VarChar, 30, "Birthdate");
sqlcommand.Parameters.Add("@Special", SqlDbType.VarChar, 100, "Special");
一、在网格视图不可编辑时,用文本框内容添加一行
//this.dashouju(数据表)是网格视图的数据源
DataRow addrow = this.dashouju.NewRow();//新建一行该数据表结构的数据行
addrow["shoujuNo"] = this.txb_jiaokuanshoujuNo.Text.Trim();//为新的一行中的每一列赋值相应文本框的内容。
addrow["zuNo"] = this.txb_jiaokuanzuNo.Text.Trim();
addrow["wherework"] = this.txb_jiaokuandanwei.Text.Trim();
addrow["wherework"] = this.txb_jiaokuandanwei.Text.Trim();
addrow["shoujumoney"] = this.txb_jiaokuanmoney.Text.Trim();
addrow["paystyle"] = this.cmb_jiaokuanpaystyle.Text.Trim();
addrow["payway"] = this.cmb_jiaokuanway.Text.Trim();
addrow["paytime"] = this.tsl_jiaokuantime.Text.Trim();
addrow["gonghaoNo"] = this.tstxb_jiaokuangonghaoNo.Text.Trim();
addrow["bshoujumoney"] = this.txb_jiaokuanmoneyb.Text.Trim();
this.dashouju.Rows.Add(addrow);//把该行的状态设置为添加
//以上只在网格视图的前端界面中添加一行,但数据库中尚未添加,再用第一个方法把新增的行存入数据库。代码如下:
sqlcommand.CommandText ="insert tb_shouju(shoujuNo,zuNo,wherework,shoujumoney,paystyle,payway,paytime,gonghaoNo,bshoujumoney) values (@shoujuNo,@zuNo,@wherework,@shoujumoney,@paystyle,@payway,@paytime,@gonghaoNo,@bshoujumoney);";
sqlcommand.Parameters.Add("@shoujuNo", SqlDbType.Char, 30, "shoujuNo");
sqlcommand.Parameters.Add("@zuNo", SqlDbType.Char, 30, "zuNo");
sqlcommand.Parameters.Add("@wherework", SqlDbType.VarChar, 0, "wherework");
sqlcommand.Parameters.Add("@shoujumoney", SqlDbType.Char, 10, "shoujumoney");
sqlcommand.Parameters.Add("@paystyle", SqlDbType.VarChar, 0, "paystyle");
sqlcommand.Parameters.Add("@payway", SqlDbType.VarChar, 0, "payway");
sqlcommand.Parameters.Add("@paytime", SqlDbType.VarChar, 0, "paytime");
sqlcommand.Parameters.Add("@gonghaoNo", SqlDbType.Char, 10, "gonghaoNo");
sqlcommand.Parameters.Add("bshoujumoney", SqlDbType.VarChar, 0, "bshoujumoney");
方法二对存入数据库的准确性有更高的保障。