Results 1 to 5 of 5

Thread: Populating DataGridView from BackgroundWorker - Invoking

  1. #1
    jsurpless is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2008
    Posts
    139
    Reputation
    63

    Populating DataGridView from BackgroundWorker - Invoking

    Hi all

    I'm trying to do this in a multi-thread

    Code:
    DataGridView.Rows.Add({Column1, Column2})
    I created a delegate and the AddressOf sub

    Code:
    Private Delegate Sub UpdateDataGridViewCallback(ByVal text As String())
    
    Private Sub UpdateDataGridView(ByVal text As String())
    
      DataGridView.Rows.Add(text)
    
    End Sub
    When I try to execute the code

    Code:
    UpdateDataGridView.Invoke(New UpdateDataGridViewCallback(AddressOf UpdateDataGridView), {Column1, Column2})
    I get 'System.Reflection.TargetParameterCountException'

    I'm assuming that I'm not specifying the array dimensions correctly or something

    Any thoughts?

    Thanks!

    -Justin

  2. #2
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    448
    Reputation
    346
    Instead of

    {Column1, Column2}

    try

    New String(){Column1, Column2}

  3. #3
    jsurpless is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2008
    Posts
    139
    Reputation
    63
    Quote Originally Posted by Herman View Post
    Instead of

    {Column1, Column2}

    try

    New String(){Column1, Column2}
    So add that to the end of the .Invoke statement?

  4. #4
    SLPx is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Apr 2012
    Location
    Sri Lanka
    Posts
    17
    Reputation
    16
    As mentioned Your delegate designed for a string array for one element and You are parsing two elements to it.

    Either change it as Herman suggests, Or change your delegate to
    UpdateDataGridViewCallback(ByVal column1 As String, column2 as string)

    or
    change your invoke to

    UpdateDataGridView.Invoke(New UpdateDataGridViewCallback(AddressOf UpdateDataGridView), new string(){Column1, Column2})
    Alternatively, If you are trying to insert rows to a DG, Rather than using its row addition one by one manual, try using the datasource property.

    Ex,
    1.Use a Shared variable As DataTable, (Or alternatively you may pass a dim variable you initiated in the calling thread with data as,
    UpdateDataGridViewCallback (datTable as data.datatable)

    2. change the necessary amendments, additions, data inserts to it in the background thread

    3.call DG invoke and assign your table to it
    dg.datasource=Table



  5. #5
    jsurpless is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2008
    Posts
    139
    Reputation
    63
    Quote Originally Posted by SLPx View Post
    As mentioned Your delegate designed for a string array for one element and You are parsing two elements to it.

    Either change it as Herman suggests, Or change your delegate to
    UpdateDataGridViewCallback(ByVal column1 As String, column2 as string)

    or
    change your invoke to

    UpdateDataGridView.Invoke(New UpdateDataGridViewCallback(AddressOf UpdateDataGridView), new string(){Column1, Column2})
    Alternatively, If you are trying to insert rows to a DG, Rather than using its row addition one by one manual, try using the datasource property.

    Ex,
    1.Use a Shared variable As DataTable, (Or alternatively you may pass a dim variable you initiated in the calling thread with data as,
    UpdateDataGridViewCallback (datTable as data.datatable)

    2. change the necessary amendments, additions, data inserts to it in the background thread

    3.call DG invoke and assign your table to it
    dg.datasource=Table


    Couldn't get this to work

    UpdateDataGridView.Invoke(New UpdateDataGridViewCallback(AddressOf UpdateDataGridView), new string(){Column1, Column2})

    with

    Private Delegate Sub UpdateDataGridViewCallback(ByVal text As String())

    Private Sub UpdateDataGridView(ByVal text As String())

    but I tried your 2nd recommendation and it worked

    UpdateDataGridView.Invoke(New UpdateDataGridViewCallback(AddressOf UpdateDataGridView), {Column1, Column2})

    UpdateDataGridViewCallback(ByVal column1 As String, column2 as string)

    Thanks!

Bookmarks

Posting Permissions

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