使用BackgroundWorker格式化DataGridView [英] Use BackgroundWorker to Format DataGridView

查看:128
本文介绍了使用BackgroundWorker格式化DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView控件,正在用数据库中的数据填充.由于查询有可能返回大量数据,因此我决定使用BackgroundWorker对象提取数据,以便查询运行时我的应用程序不会冻结.我可以使其正常工作,但并不能解决我的整体问题.提取数据后,我为其设置了DataGridView的数据源,然后调用一个方法来设置DataGridView对象的所有格式.它遍历列和行,并根据数据对单元格进行设置背景颜色,对齐方式和工具提示之类的操作.这都是非常耗时的,并且我的应用程序在运行时似乎停滞了.但是,尝试将其放入BackgroundWorker时遇到了麻烦.我尝试了两种方法...

方法一:
如果我将DataGridView对象作为BackgroundWorker中的参数传递,则在尝试设置DataSource时会遇到跨线程错误.这就是我总是将事情打包"到后台工作人员的方式,而在...我没有正确执行之前,我还没有遇到问题吗?

I have a DataGridView control that I’m filling with data from a database. Since the queries have the possibility of returning a lot of data I decided to use a BackgroundWorker object to pull the data so my application wouldn’t appear to be frozen when the query was running. I can get that to work just fine but it didn’t solve my overall problem. After pulling the data I set the DataGridView’s Datasource to it and then call a method that sets up all kinds of formatting on the DataGridView object. It loops through the columns and rows and does things like set background color, alignment, and tool tips on cells based on the data. This is all quite time consuming and my application appears to freeze up when it runs. However I’m having trouble trying to get this into a BackgroundWorker. I’ve tried two methods...

Method One:
If I pass in my DataGridView object as an argument in the BackgroundWorker I get a cross-threading error when I try to set the DataSource. This is how I''ve always "packaged" things to go into a background worker and I haven''t run into problems before...am I not doing it correctly?

'The class I use to package the DataGridView
Private Class packageFormatGrid
      Public dt As DataTable 'Data to set to the grid
      Public dgv1 As DataGridView 'Data grid
End Class
'The class I package the datagridview in and call backgroundworker from
Private Sub CallFormat(ByRef dt as DataTable)
       Dim package As New packageFormatGrid
       package.dt = dt
       package.dgv1 = dgvOutput
       bgwFormat.RunWorkerAsync(package)
End Sub
'Do Work method...
Private Sub bgwFormat_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwFormat.DoWork
       Dim package As packageFormatGrid = CType(e.Argument, packageFormatGrid)
       package.dgv1.DataSource = package.dt
       'I have other code after this point, but the above statement causes
       'a cross-threading error for dgvOutput, which is the "packaged" dgv1
End Sub



方法二:
如果我声明一个新的DataGridView对象并将其作为结果传递给RunWorkerComplete,并且在其中将我的实际DataGridView设置为等于它,则不会出现任何错误,但是我的网格根本不会填满任何内容.



Method Two:
If I declare a new DataGridView object and pass it to the RunWorkerComplete as the result, and there set my real DataGridView equal to it, I don’t get any errors, but my grid simply doesn’t fill with anything.

'Set up a new datagridview object and pass it back to other thread in result
Private Sub bgwFormat_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwFormat.DoWork
       Dim package As packageFormatGrid = CType(e.Argument, packageFormatGrid)
       Dim dgv As New DataGridView
       dgv.DataSource = package.dt
       'method that loops columns and rows and formats the datagrid view object
       FormatOutputGrid(dgv)
       e.Result = dgv
End Sub
'On run worker complete, the code doesn't error out, but my grid doesn't display any data.
'Debugging reveals that it has the correct DataSource, but I can't access cell values
Private Sub bgwFormat_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwFormat.RunWorkerCompleted
       Dim dgv As DataGridView = CType(e.Result, DataGridView)
       dgvOutput = dgv
End Sub



现在是星期一,所以我怀疑自己的思路不清晰,而且我想念的事情也很明显.当我用google搜索时,我得到的只是有关使用BackgroundWorker提取数据的文章,而与格式化网格无关.任何人有任何想法吗?



It’s Monday, so I’m suspicious that I’m just not thinking clearly and I’m missing something painfully obvious. When I google all I get is articles about pulling the data with a BackgroundWorker and nothing about formatting the grid. Does anyone have any ideas?

推荐答案

您不能从后台线程修改GUI控件.对GUI控件的所有更改都必须在创建控件的线程(即启动线程)上进行.这包括所有格式更改,甚至包括分配DataSource属性.

任何格式化都应通过列属性完成,该过程不会花费很长时间.同样,您也不应该遍历所有行的设置内容.应按需通过处理鼠标,格式和绘画事件来完成此操作.
You can NOT modify a GUI control from a background thread. All changes to a GUI control have to be done on the thread that created the control, which is the startup thread. This includes any formatting changes and even assigning the DataSource property.

Any formatting should be done by column properties, which don''t take very long. You should, also, not be going through all of the rows setting stuff. This should be done, on demand, by handling mouse, format, and painting events.


这篇关于使用BackgroundWorker格式化DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆