Results 1 to 3 of 3

Thread: [RESOLVED] Updating a UI DataGridView from A DataTable That's Appended Via Threading

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2022
    Posts
    39

    Resolved [RESOLVED] Updating a UI DataGridView from A DataTable That's Appended Via Threading

    I am trying to append new columns to a DataGridView which resides on a Form1 TabControl, based on new columns that are added to a global DataTable (dt) in a threaded class.

    The delegate to update the the DGV on the TabControl in Form1 is defined as global in a declarations module:

    Code:
    Public Delegate Sub AppendDGV1Delegate(ByVal OrigNumCols As Integer, ByVal j As Integer)
    In the threaded work method, the syntax to first add the columns to the dt, then add the row-data to the columns in the dt, and then update the DGV via the delegate is
    Code:
    Dim main As Form1 = CType(Application.OpenForms(0), Form1)
    
    'NumNewCols is not zero-based
    'OrigNumCols is not-zero-based
    For j = 1 To NumNewCols                
       dt.Columns.Add(NewColName(OrigNumCols + j), 
    Next
    For j = 1 To NumNewCols
      For i = 0 To NumRows-1
        dt.Rows(i)(OrigNumCols + j - 1) = x(i, j)
      Next
      main.Invoke(New AppendDGV1Delegate(AddressOf main.updateDGV1), OrigNumCols, j) 
    Next j
    The syntax inside the Sub called by the delegate in Form1 is

    Code:
    Public Sub updateDGV1(ByVal OrigNumCols As Integer, ByVal j As Integer)
      CType(TabControl2.TabPages(1).Controls("dgv1"), DataGridView).InvalidateColumn(OrigNumCols + j - 1)
      CType(TabControl2.TabPages(1).Controls("dgv1"), DataGridView).Refresh()
    End Sub
    If I don't thread the work method, the DGV is updated. However, if I thread the work method, then the DGV simply shows the column headers with blank cells.

    Do I need to do more to append new columns in a DGV that resides on a Form1 TabControl, after the new columns are appended to the dt.

    FYI - at no time does using

    Code:
    CType(main.TabControl2.TabPages(1).Controls("dgv1"), DataGridView).DataSource = dt
    help this issue, since the DataSource was defined earlier. (Note, this works when threading is not used, so .DataSource has nothing to do with the issue).
    Last edited by pel11; Mar 18th, 2022 at 03:33 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,896

    Re: Delegate Updating a UI DataGridView from A DataTable That's Appended Via Threadin

    In Form1...

    Code:
    Public Sub updateDGV1(ByVal OrigNumCols As Integer, ByVal j As Integer)
        Dim dgv As DataGridView = CType(TabControl2.TabPages(1).Controls("dgv1"), DataGridView)
        If dgv.InvokeRequired Then
            dgv.Invoke(New Action(Of Integer, Integer)(AddressOf updateDGV1), OrigNumCols, j)
        Else
            dgv.InvalidateColumn(OrigNumCols + j - 1)
            dgv.Refresh()
        End If
    End Sub
    In your secondary thread...

    Code:
    main.updateDGV1(OrigNumCols, j)
    Last edited by .paul.; Mar 18th, 2022 at 05:12 AM.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2022
    Posts
    39

    Re: Delegate Updating a UI DataGridView from A DataTable That's Appended Via Threadin

    I found out via programming that any code that modifies the dt inside the threaded method, needs to be moved to a method in Form1 (UI), which is called via delegate from the threaded method that generates the new data. Once that's done, the delegated dgv.ValidateColume(ColNum), followed by dgv.Refresh will work. In other words, you cannot modify a dt in a threaded method, and expect a DGV in the UI (Form1) to become updated. Changes to a dt whose data are used for updating a DGV in Form1 (UI) can only be made by delegated calls to methods in Form1 which modify the dt first, and then delegated methods in Form1 (UI) which update the DGV. Thus you can't use any code to refer to the dt or DGV inside the threaded work method. All modifying code has to be in methods in Form1.
    Last edited by pel11; Mar 18th, 2022 at 03:42 PM.

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