2
複数のデータグリッドビューの変更をユーザーが変更すると自動的にデータベースに保存しようとしています。ボタンのクリックイベントに結び付けると、私のコードはうまく動作しますが、cellvaluechangedイベントに結びつけてしまうと、うまく動作しません。何か案は?それでもコードをステップ実行し、エラーをスローしません。データベースを更新しません。DataGridView 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