Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27353

[RESOLVED] Adding Handler to DataGridView doesn't run code correctly

$
0
0
Hello,

In my project when i dynamically create a DataGridView I am adding a Handler to the CellParsing event wit the following line:
Code:

AddHandler DatGr.CellParsing, (AddressOf Datgr_End_Update)
My code in the Sub Routine Datgr_End_Update is an update command that updates my database when a value in the DataGridView is changed by the user.
Code:

Private Sub Datgr_End_Update(sender As System.Object, e As System.EventArgs)

        Dim tc_1 As TabControl
        Dim dg_1 As New DataGridView

        For Each Ctrl_1 As Control In tcTest.SelectedTab.Controls
            If TypeOf Ctrl_1 Is TabControl Then
                tc_1 = Ctrl_1
                For Each Ctrl_2 As Control In tc_1.SelectedTab.Controls
                    If TypeOf Ctrl_2 Is DataGridView Then
                        dg_1 = Ctrl_2
                        sql = "UPDATE Skills SET empno=@empno,prod=@prod,subassy=@subassy,opno=@opno,competence=@competence" _
                            & ",DateAchieved=@DateAchieved,Stage=@stage Where OpNo = @opno"
                        Dim row As DataGridViewRow = dg_1.CurrentRow

                        da.UpdateCommand = New OleDbCommand(sql, con)
                        da.UpdateCommand.Parameters.AddWithValue("@empno", row.Cells("empno").Value)
                        da.UpdateCommand.Parameters.AddWithValue("@prod", row.Cells("prod").Value)
                        da.UpdateCommand.Parameters.AddWithValue("@subassy", row.Cells("subassy").Value)
                        da.UpdateCommand.Parameters.AddWithValue("@opno", row.Cells("opno").Value)
                        da.UpdateCommand.Parameters.AddWithValue("@competence", row.Cells("competence").Value)
                        da.UpdateCommand.Parameters.AddWithValue("@DateAchieved", row.Cells("DateAchieved").Value)
                        da.UpdateCommand.Parameters.AddWithValue("@Stage", row.Cells("stage").Value)
                        da.Update(ds.Tables("" & SubAss & " " & EmpNumber & ""))
                        MsgBox("Record has been updated")
                    End If
                Next
            End If
        Next

    End Sub

However when I edit a value in my DataGridView the code runs, and the MsgBox pops up but the database doesn't get updated.

The strange thing is that when i put the code behind a button the code works perfectly and the database is updated :confused:

Am I creating the Handler correctly? I think that i am as when the cell value has changed the code in the Sub Routine runs, but why doesn't my database get updated?

Thank you for your time!!!

Viewing all articles
Browse latest Browse all 27353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>