回答对了再多给分:(Win Form)对DataGrid的多行增加、删除、修改 有个最简单的方法就是建立一个XML 数据集,通过操作数据集来操作。最后统一更新数据集 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你插入的代码是什么?会不会是那里错了?你连续加入多行没有主健吗?按理说是可以的。关于3.iRow=dg.CurrentRowIndex; 看如下的方法:首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");conn.Open();System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);dt = new System.Data.DataSet();da.Fill(dt,"student");然后绑定数据集和DataGridDataGrid.SetDataBinding(dt,"student");如果需要,可以绑定TextBox来作录入,而用DataGrid显示this.textBox16.DataBindings.Add("Text",dt,"student.stuno");然后进行数据的操作如:增加:this.BindingContext[dt,"student"].AddNew();删除:this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);最后把结果写回数据库:// sqlInsertCommand1// this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)"; this.sqlInsertCommand1.Connection = this.conn;this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));// // sqlUpdateCommand1// this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";this.sqlUpdateCommand1.Connection = this.conn;this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null)); // sqlDeleteCommand1// this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";this.sqlDeleteCommand1.Connection = this.conn;this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null)); this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;this.sqlDa.InsertCommand = this.sqlInsertCommand1;this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;try{ sqlDa.Update(dt.GetChanges,"student"); return true;}catch(System.Data.SqlClient.SqlException ex){ return false;} finally{ conn.Close();} 楼主在更新数据库之前要通知界面停止编辑。// 停止当前的任何编辑。this.BindingContext[ds1,"tableName"].EndCurrentEdit();如果上面不行那就这样做吧;this.BindingContext[ds1,"tableName"].Position = 0;this.BindingContext[ds1,"tableName"].Position = this.BindingContext[ds1,"tableName"].Count-1;上面的代码好比是在DataGrid上的小箭头移动了一下。 @是SQl数据提供者参数定义标志! 可以使用循环删除方法,先用for循环检测每一行的删除条件(如column.checkbox.selected=true),如果符合条件就删除,这个方法比较苯,但是我暂时想不到好的方法了. to:TwoEyes完成数据的插入后,加:myAdapter.InsertCommand.ExecuteNonQuery();使所有的操作生效 在 snof(雪狼)的回答中对stuno我不想手工在datagrid中录入,我想那列隐藏,动态的赋值,即stuno的值是递增的。怎么实现???? 如何把查询到的数据从datatable用for循环加载到datagridview? C#应用程序加载ActiveX控件释放时蓝屏 C# 切换音轨代码 求助,一个问题难倒了我两天! xml文件操作问题请教 visio只有“反向工程”,那么做好的E-R图,怎么把表从数据库里建起来?? 如何用c#怎么获得系统的分辩率 如何用c++通过串口控制 三盏LED灯亮与灭? 简单问题:C#里怎么判断输入的字符是全角还是半角? C#中怎样实现FTP文件传输,应该用那些类,有参考实例吗?多谢!!! 关于两个用户控件间传递消息的问题 CrystalReportViewer时要求签出工程问题
你连续加入多行没有主健吗?按理说是可以的。
关于3.
iRow=dg.CurrentRowIndex;
首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{
return false;
}
finally
{
conn.Close();
}
// 停止当前的任何编辑。
this.BindingContext[ds1,"tableName"].EndCurrentEdit();如果上面不行那就这样做吧;
this.BindingContext[ds1,"tableName"].Position = 0;
this.BindingContext[ds1,"tableName"].Position = this.BindingContext[ds1,"tableName"].Count-1;
上面的代码好比是在DataGrid上的小箭头移动了一下。
使所有的操作生效
对stuno我不想手工在datagrid中录入,我想那列隐藏,动态的赋值,即stuno的值是递增的。
怎么实现????