如何使用BackGroundWorker刷新DataGridView

问题描述 投票:0回答:1

我正在尝试创建一个接收文件路径列表并按顺序打印所有这些文件的应用程序。在打印文件时,您可以取消打印或在列表中移动文件。一切正常,但我不知道每次更新后如何刷新DataGridView。所以请有人可以帮我吗?这是我的代码,我尝试使用BackgroundWorker_ProgressChanged更新datagridview,但我知道这不是最好的方法。

Private Sub btnCancelPrint_Click(sender As Object, e As EventArgs) Handles btnCancelPrint.Click
    Dim msg = "Annullare la stampa di tutti i documenti?"
    Dim title = "Annulla Stampa"
    Dim style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or
                MsgBoxStyle.Critical
    Dim response = MsgBox(msg, style, title)
    If response = MsgBoxResult.Yes Then
        If BackgroundWorkerPrint.IsBusy Then
            If BackgroundWorkerPrint.WorkerSupportsCancellation Then
                btnPrint.Enabled = True
                btnCancelPrint.Enabled = False
                BackgroundWorkerPrint.CancelAsync()
            End If
        End If
    End If

End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    Try
        If BackgroundWorkerPrint.IsBusy <> True Then
            btnPrint.Enabled = False
            BackgroundWorkerPrint.RunWorkerAsync()
        End If
    Catch ex As Exception
    End Try
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerPrint.DoWork
    printFont = New Font("Arial", 10)
    Dim pd As New PrintDocument()
    Dim pDialog As New PrintDialog()
    Dim ps As New PrinterSettings()
    Dim psize As New PaperSize()
    pd.DefaultPageSettings.Landscape = False
    pDialog.Document = pd
    pDialog.Document.DefaultPageSettings.PaperSize = psize
    AddHandler pd.PrintPage, AddressOf PrintDoc_PrintPage
    Dim result As New DialogResult
    result = pDialog.ShowDialog()
    BackgroundWorkerPrint.ReportProgress(0)
    BackgroundWorkerPrint.ReportProgress(4)
    For Each fattura As Fattura In DataGridFatture.DataSource
        Thread.Sleep(10000)
        If BackgroundWorkerPrint.CancellationPending Then
            BackgroundWorkerPrint.ReportProgress(2)
            e.Cancel = True
            Exit For
        End If
        If (fattura.Stato <> Fattura.Semaforo.STAMPATO) Then
            pathDoc = fattura.Path
            BackgroundWorkerPrint.ReportProgress(3)
            Try
                streamToPrint = New StreamReader(fattura.Path)
                Try
                    If (result = DialogResult.OK) Then
                        pd.Print()
                        BackgroundWorkerPrint.ReportProgress(1)
                    End If
                Finally
                    streamToPrint.Close()
                End Try
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    Next
    BackgroundWorkerPrint.ReportProgress(5)
End Sub
Private Sub BackgroundWorkerPrint_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorkerPrint.ProgressChanged
    Select Case e.ProgressPercentage
        Case 0
            For Each dr As Fattura In DataGridFatture.DataSource
                If (dr.Stato <> Fattura.Semaforo.STAMPATO) Then
                    dr.Stato = Fattura.Semaforo.INATTESA
                    dr.Icon = My.Resources.Waiting_icon
                    DataGridFatture.Refresh()
                End If
            Next
        Case 1
            For Each dr As Fattura In DataGridFatture.DataSource
                If (dr.Path = pathDoc) Then
                    dr.Stato = Fattura.Semaforo.STAMPATO
                    dr.Icon = My.Resources.Ok_Icon
                    DataGridFatture.Refresh()
                End If
            Next
        Case 2
            For Each dr As Fattura In DataGridFatture.DataSource
                If (dr.Stato = Fattura.Semaforo.INSTAMPA Or dr.Stato = Fattura.Semaforo.INATTESA) Then
                    dr.Stato = Fattura.Semaforo.ANNULLATO
                    dr.Icon = My.Resources.Cancel_icon
                    DataGridFatture.Refresh()
                End If
            Next
        Case 3
            For Each dr As Fattura In DataGridFatture.DataSource
                If (dr.Path = pathDoc) Then
                    dr.Stato = Fattura.Semaforo.INSTAMPA
                    dr.Icon = My.Resources.Print_icon
                    DataGridFatture.Refresh()
                End If
            Next
        Case 4
            btnCancelPrint.Enabled = True
        Case 5
            btnPrint.Enabled = True
    End Select
End Sub

Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
    If (bs.Count <= 1) Then Return

    Dim position As Integer = bs.Position
    If (position = bs.Count - 1) Then Return

    bs.RaiseListChangedEvents = False

    Dim current As Fattura = bs.Current
    bs.Remove(current)

    position = position + 1

    bs.Insert(position, current)
    bs.Position = position

    bs.RaiseListChangedEvents = True
    bs.ResetBindings(False)
End Sub
c# vb.net winforms
1个回答
0
投票

问题是,数据网格不属于后台进程,但属于另一个进程,即表单。后台进程具有自己的资源,无法直接访问表单控件!无论如何,请尝试:

System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false

但是请注意,此指令破坏了安全策略。

© www.soinside.com 2019 - 2024. All rights reserved.