问一个通过编辑datagridview修改数据表的问题
通过代码将datagridview绑定到数据源 dataGridView1.DataSource =DataSet1.Tables("result")然后希望用户在datagridview里面加入或者修改数据以后,按button1可以将编辑后的数据传递到数据库的表中去
请问button1的实现代码应该如何写?谢谢
--------------------编程问答-------------------- 补充一下 DataSet1的select语句是strSql="select * from 人事表 where 科室='"& TextBox1.Text &"'" --------------------编程问答-------------------- 用VS2005可以直接绑定提交数据啊 --------------------编程问答-------------------- 獲取你修改時那一條數據的焦點的數據。然後用sql語句把它更新到數據庫。
獲取你選中的數據datagridview.SelectedRows(0).Cells(3).Value.ToString.Trim
獲取焦點的數據datagridview.CurrentRow.Cells(0).Value.ToString.Trim --------------------编程问答-------------------- SqlCommand myCommand = new SqlCommand(" select * from result", conn);//conn 是连结来的
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand );
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.InsertCommand = myCommandBuilder .GetInsertCommand();
myAdapter.UpdateCommand = myCommandBuilder .GetUpdateCommand();
myAdapter.DeleteCommand = myCommandBuilder .GetDeleteCommand();
myAdapter.Update(ds, "result"); --------------------编程问答-------------------- 帮顶 --------------------编程问答--------------------
Dim myCommand As New SqlCommand("select * from 人事表 where 科室='"& TextBox1.Text &"'", conn)
'conn 是连结来的
Dim myAdapter As New SqlDataAdapter(myCommand)
Dim myCommandBuilder As New SqlCommandBuilder(myAdapter)
myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand()
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand()
myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand()
myAdapter.Update(DataSet1, "result")
conn是你的连接对象 --------------------编程问答-------------------- 忘记说了:要保证“人事表”中有主键,否则会出错。 --------------------编程问答-------------------- 顶了~~~
刚习惯了用recordset,
dataset不太会。。。
补充:.NET技术 , VB.NET