Populating DataGridView from BackgroundWorker - Invoking

jsurpless

Well-known member
Joined
Jul 29, 2008
Messages
144
Programming Experience
Beginner
Hi all

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

VB.NET:
DataGridView.Rows.Add({Column1, Column2})

I created a delegate and the AddressOf sub

VB.NET:
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

VB.NET:
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
 
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


 
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!
 
Back
Top