[英]How to refresh DataGridView with BackGroundWorker
I'm trying to create an application that receives aa list of file paths and print in sequence all of this files.我正在尝试创建一个接收文件路径列表并按顺序打印所有这些文件的应用程序。 While printing the files you can cancel printing or move the files within the list.
打印文件时,您可以取消打印或在列表中移动文件。 Everything works good but i don't know how to refresh the DataGridView after every update.
一切正常,但我不知道如何在每次更新后刷新 DataGridView。 So please someone could help me?
那么请有人可以帮助我吗? Here is my code, i tried to use BackgroundWorker_ProgressChanged to update the datagridview But I know it's not the best thing to do.
这是我的代码,我尝试使用 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
The problem is that the datagrid doesn't belong to the background process but it's got to another process, your form.问题是数据网格不属于后台进程,但它属于另一个进程,即您的表单。 Background process has its own resources and cannot access directly to the form controls, Anyway: try with:
后台进程有自己的资源,不能直接访问表单控件,无论如何:尝试:
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false
but attention, this instruction breaks the security policy.但请注意,此指令违反了安全策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.