Refresh a datagridview

Claude Larocque 666 Reputation points
2022-04-05T20:24:15.007+00:00

Hi everyone,
I try so many things to refresh my datagridview without success so I decided to ask a well detailed question and hopefully we will find the right way.

I have a pdf document attach and in this document everything is well explain,
Thanks in advance
Claude190220-why-is-it-so-hard-to-refresh-a-datagridview.pdf

    Public Sub UpdateSalespersonIDInOrderLine()  
        'ADD SQL PARAMS & RUN THE COMMAND  
        Try  
            SQL.AddParam("@OrderLine", Me.OrderLineIDTB.Text)  
            SQL.AddParam("@SalespersonID", Me.SalespersonIDTB.Text)  
            SQL.ExecQuery("UPDATE Sales.OrderLines " &  
                                  "SET SalespersonID=@SalespersonID WHERE OrderLineID=@OrderLine;")  
            RecordsCounted()  
            ProductCBX.Select()  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Public Sub BtnUpdateSalesperson_Click(sender As Object, e As EventArgs) Handles BtnUpdateSalesperson.Click  
        Try  
            UpdateSalespersonIDInOrderLine()  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2022-04-06T10:00:18.923+00:00

    I have tried to put the BtnUpdateSalesperson.PerformClick many places but without success. If I click on that button, everything works perfectly

    I don't care for PerformClick, a better method is to take the code in the Click event for BtnUpdateSalesperson and place the code into a procedure and then in the Click event call the procedure.

    So for a conceptual example let's say the code in Work has been moved from Button1_Click so now, Button2 can execute that code by calling Work.

    Public Class ExampleForm
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Work()
        End Sub
    
        Private Sub Work()
            MessageBox.Show("Working")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If True Then
                Work()
            End If
        End Sub
    End Class
    

2 additional answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,426 Reputation points Microsoft Vendor
    2022-04-06T06:09:38.817+00:00

    Hi @Claude Larocque ,

    After the customer is chosen, another form opens automatically to allow users to select a salesperson for that OrderLinesDGV row. This update the ID of the salesperson on the main form (FrmBilling) That field is “SalespersonIDTB.Text “.

    Since you can update Salesperson IDTB.Text after selecting salesperson, you can also choose to update OrderLinesDGV at the same time.
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Claude Larocque 666 Reputation points
    2022-04-06T13:39:10.54+00:00

    Ok some update, if I add a row with the product tattoo, I still have to manually press the BtnUpdateSalesperson to have the new row updated, however, I put a code on the FrmSalesperson on FormClosed:

        Private Sub FrmSalespersons_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
            Try
                If FrmBilling IsNot Nothing Then
                    FrmBilling.UpdateSalespersonIDInOrderLine()
                End If
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
                MsgBox(ex.Message)
                System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)
            End Try
        End Sub
    

    Now if a create a second row with tattoo, then the SalespersonID for the first row is automatically update but not the second row (New row) I tried to put the code on the OrderLinesDGV added rows it does not work.

    I guess they will have not to forget to choose manually the salesperson for that specific row :(

    0 comments No comments