0

i got a background worker that has the following code in the do work.... one of the things i want to do is to add rows if theres none available ...but i got an error saying i need to do a safe call thread...i already read about it but im stuck i put control.invoke, but that gives me an error too saying Error 1 Reference to a non-shared member requires an object reference.


so i need help what can i do to add rows safely

Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    
                        If DataGridView2.Rows.Item(num).Cells.Item(0).Value = "" Then
                            Control.Invoke(DataGridView2.Rows.Add(1))
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Teme64
0

Use SyncLock...End SyncLock to prevent other threads to update rows at the same time

Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    
SyncLock DataGridView2.Rows
  If DataGridView2.Rows.Item(num).Cells.Item(0).Value = "" Then DataGridView2.Rows.Add(1)
End SyncLock

Read more about the SyncLock Statement in the MSDN.

HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.