Results 1 to 5 of 5

Thread: [RESOLVED] Using a Databound DGV Find and update a record or insert it

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    91

    Resolved [RESOLVED] Using a Databound DGV Find and update a record or insert it

    I have a data-bound DataGridView with a BindingSource and a TableAdapter all together it makes two column leaderboard linking back to an Access DB

    I need to be able to check whether a particular record exists and, if it does exist, then update it with new info. If it does not exist then add it.

    My code so far after many permutations and still not working is:

    Code:
       Private Sub UpdateLeaderboard(ByVal jName As String, jWins As Integer)
            Dim test As Boolean = False
            'Check whether the name already exists
            Dim jRow As DataRow = SsrDataSet.JockeyLeaderboard.NewRow
            For Each row In JockeyLeaderboardDataGridView.Rows
                If jName = row.Cells(0).Value Then
                    jWins = row.Cells(1).Value
                    test = True
                    jWins += 1
                    Exit For
                End If
            Next
    
            If test = True Then
                'update the recoed with new info
    
            End If
            If test = False Then
                'the name is not present so add it
                Dim MyNewRow As DataRow
                MyNewRow = SsrDataSet.JockeyLeaderboard.NewRow
                With MyNewRow
                    .Item(0) = jName
                    .Item(1) = jWins
                End With
                SsrDataSet.JockeyLeaderboard.Rows.Add(MyNewRow)
                SsrDataSet.JockeyLeaderboard.AcceptChanges()
            End If
            Try
                Me.Validate()
                Me.JockeyLeaderboardBindingSource.EndEdit()
                Me.JockeyLeaderboardTableAdapter.Update(Me.SsrDataSet.JockeyLeaderboard)
                'MsgBox("Update successful")
                My.Computer.Audio.Play(My.Resources.notify, AudioPlayMode.Background)
            Catch ex As Exception
                MsgBox(ex)
            End Try
        End Sub
    At present it will add a new record to the DGV but not save it to the database and I haven't found a reliable way of updating the dataset/table


    Please point out the error of code

    Thanks
    Last edited by Penfound; May 15th, 2018 at 08:15 AM. Reason: Correct Title

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width