2016-03-29 3 views
2

Ich versuche, Änderungen an mehreren Datagridviews automatisch in meiner Datenbank zu speichern, wenn Benutzer Änderungen vornehmen. Der Code, den ich habe, funktioniert gut, wenn ich es an ein Button-Click-Ereignis binde, aber nicht, wenn ich es an ein cellvaluechanged-Ereignis binde. Irgendwelche Ideen? Es geht immer noch durch den Code, und es wirft keine Fehler auf, es aktualisiert die Datenbank einfach nicht.DataGridView In Datenbank bei CellValueChanged Ereignis speichern

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 

Antwort

0

Dieser Code wurde hinzugefügt und das Problem wurde behoben.

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