我尝试在用户进行更改时自动将多个datagridviews上的更改保存回我的数据库。如果我把它绑定到按钮单击事件,我的代码工作正常,但是当我将它绑定到cellvaluechanged事件时。有任何想法吗?它仍然会逐步执行代码,并且不会抛出任何错误,它只是不会更新数据库。
Private dgv1_Source As New BindingSource()
Private dgv1_Adapter As New MySql.Data.MySqlClient.MySqlDataAdapter()
Private dgv2_Source As New BindingSource()
Private dgv2_Adapter As New MySql.Data.MySqlClient.MySqlDataAdapter()
Private dgv3_Source As New BindingSource()
Private dgv3_Adapter As New MySql.Data.MySqlClient.MySqlDataAdapter()
Private dgv4_Source As New BindingSource()
Private dgv4_Adapter As New MySql.Data.MySqlClient.MySqlDataAdapter()
Private Sub MainLoad(sender As Object, e As EventArgs) Handles MyBase.Load
'Grid 1
dgv1.DataSource = dgv1_Source
Dim selectCommand As String = "SELECT * FROM Table1"
dgv1_Adapter = New MySql.Data.MySqlClient.MySqlDataAdapter(selectCommand, SQL_ConnectionString)
Dim commandBuilder As New MySql.Data.MySqlClient.MySqlCommandBuilder(dgv1_Adapter)
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
dgv1_Adapter.Fill(table)
dgv1_Source.DataSource = table
'Grid 2
dgv2.DataSource = dgv2_Source
selectCommand = "SELECT * FROM Table2"
dgv2_Adapter = New MySql.Data.MySqlClient.MySqlDataAdapter(selectCommand, SQL_ConnectionString)
commandBuilder = New MySql.Data.MySqlClient.MySqlCommandBuilder(dgv2_Adapter)
table = New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
dgv2_Adapter.Fill(table)
dgv2_Source.DataSource = table
'Grid 3
dgv3.DataSource = dgv3_Source
selectCommand = "SELECT * FROM Table3"
dgv3_Adapter = New MySql.Data.MySqlClient.MySqlDataAdapter(selectCommand, SQL_ConnectionString)
commandBuilder = New MySql.Data.MySqlClient.MySqlCommandBuilder(dgv3_Adapter)
table = New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
dgv3_Adapter.Fill(table)
dgv3_Source.DataSource = table
'Grid 4
dgv4.DataSource = dgv4_Source
selectCommand = "SELECT * FROM Table4"
dgv4_Adapter = New MySql.Data.MySqlClient.MySqlDataAdapter(selectCommand, SQL_ConnectionString)
commandBuilder = New MySql.Data.MySqlClient.MySqlCommandBuilder(dgv4_Adapter)
table = New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
dgv4_Adapter.Fill(table)
dgv4_Source.DataSource = table
End Sub
Private Sub CellValueChangedEvent(ByVal sender As DataGridView, e As DataGridViewCellEventArgs) Handles dgv1.CellValueChanged, dgv2.CellValueChanged, _
dgv3.CellValueChanged, dgv4.CellValueChanged
Try
'Perform Calculations
SaveAll() 'This one does not work. No errors are thrown.
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ButtonClickEvent() Handles Button1.Click
SaveAll() 'This one works
End Sub
Private Sub SaveAll()
dgv1_Adapter.Update(CType(dgv1_Source.DataSource, DataTable))
dgv2_Adapter.Update(CType(dgv2_Source.DataSource, DataTable))
dgv3_Adapter.Update(CType(dgv3_Source.DataSource, DataTable))
dgv4_Adapter.Update(CType(dgv4_Source.DataSource, DataTable))
End Sub
答案 0 :(得分:0)
添加了此代码,它解决了问题。
Private Sub SaveAll()
dgv1_Source.EndEdit()
dgv2_Source.EndEdit()
dgv3_Source.EndEdit()
dgv4_Source.EndEdit()
dgv1_Adapter.Update(CType(dgv1_Source.DataSource, DataTable))
dgv2_Adapter.Update(CType(dgv2_Source.DataSource, DataTable))
dgv3_Adapter.Update(CType(dgv3_Source.DataSource, DataTable))
dgv4_Adapter.Update(CType(dgv4_Source.DataSource, DataTable))
End Sub