我是VB的新手。我想在dataGridView中进行任何更改时更新我的数据库。任何人都可以给我详细介绍吗? 方案是我将从datagridview更改值,在我单击更新按钮后我应该更改数据库value.code是
Private Sub btnModify_Click(sender as Object,e As EventArgs)处理btnModify.Click
Dim cmdbuilder As New Odbc.OdbcCommandBuilder(da)
ds = gridDisplay(cmbxState.SelectedItem)
Try
da.Update(ds.Tables("districtMaster"))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
您必须在DataAdapter中定义InsertCommand
。然后,当您想要保存更改时,您需要调用适配器的Update方法。
在表单加载上填充DataGridView
Private myConString As String
Private con As OleDbConnection = New OleDbConnection
Private Dadapter As OleDbDataAdapter
Private DSet As DataSet
Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myConString = "Your connection string here"
con.ConnectionString = myConString
con.Open()
Dadapter = New OleDbDataAdapter("SELECT * from Table", con)
DSet = New DataSet
Dadapter.Fill(DSet, "Table")
DataGridView1.DataSource = DSet.Tables("Table")
con.Close()
End Sub
更新数据库
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using con = new New OleDbConnection(myConString)
con.Open()
Dadapter.Update(DSet, "Table")
End Using
End Sub
来自文档:
<强> http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update%28v=VS.100%29.aspx 强>