在BackgroundWorker中使用ImageColumn填充DataGridView

时间:2014-09-09 00:40:59

标签: vb.net datagridview backgroundworker

我想填写 BackgroundWorker

中的 dgv1

我尝试过与this solution by MrCoDeXeR类似的事情:

Delegate Sub SetRow(ByVal row As DataGridViewRow)

Private Sub AddRow(ByVal row As DataGridViewRow)
    If Me.dgv1.InvokeRequired Then
        Dim d As New SetRow(AddressOf AddRow)
        Me.Invoke(d, New Object() {row})
    Else
        Me.dgv1.Rows.Add(row)
    End If
End Sub

Private Sub bgw1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw1.DoWork

   '...        

    Dim imgcol1 As New DataGridViewImageColumn
    Dim tmpDGV As New DataGridView
    Dim row As DataGridViewRow = Nothing

    With tmpDGV
        .Columns.Insert(0, imgcol1)
        .Columns.Add("name", "")
        .Columns.Add("path", "")
    End With

    For Each fi In f1_tab

        If (bgw1.CancellationPending = True) Then
            e.Cancel = True
            Exit For
        End If

    '...
        If found Then

              row = New DataGridViewRow
              tmpDGV.Rows.Add(exeIcon, fi.Name, fi.FullName)
              row = tmpDGV.Rows(tmpDGV.Rows.Count - 1)
              AddRow(row)

              Threading.Thread.Sleep(1)

        End If

        If row IsNot Nothing Then
               row = Nothing
        End If

        bgw1.ReportProgress(src_progress)

    Next
End Sub

但是我在 Me.dgv1.Rows.Add(行)中得到例外:

  

提供的行已经属于DataGridView控件。

如何解决这个问题?或者也许我应该以不同的方式做到这一点?

1 个答案:

答案 0 :(得分:0)

您要添加两次行。

tmpDGV.Rows.Add(exeIcon, fi.Name, fi.FullName)   'First time
row = tmpDGV.Rows(tmpDGV.Rows.Count - 1)
AddRow(row) 'Second time 

尝试仅在row对象中设置值,而不是在tmpdDGV中再次添加。