使用BackGroundWorker从另一个窗体更新DataGridView [英] Updating DataGridView from another Form using BackGroundWorker

查看:207
本文介绍了使用BackGroundWorker从另一个窗体更新DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



然后我有另一个表单(AdminForm),我需要用它来表示一个表单(WorkerForm)每当有新的数据可用时更新它的数据网格。我可以在AdminForm中查询数据库,但考虑到LoginForm已经在进行一些轮询,这可能代价高昂,我只想表示AdminForm用新数据更新DataGridView。



您可能会问,为什么LoginForm在您显示AdminForm中显示的内容时进行轮询?那么,LoginForm实际上是处理数据并通过串行端口发送它:$ heheh。我希望它能够在没有管理员登录的情况下处理数据。 你可以有你的背景工人信号进度( WorkerReportsProgress 属性设置为true,然后调用 ReportProgress )。 在该事件处理程序( OnProgressChanged )中,调用另一个公共事件,该事件是信号接收器的数据应该更新。因为我不太确定是否调用了 OnProgressChanged 事件在主线程或后台工作线程的上下文中,我建议您在实际进行UI更新时使用 this.Invoke



编辑

使用 OnProgressChanged 和另一个事件,请执行以下操作: / p>

Form1 类中声明一个新事件,如

 公共事件EventHandler DataChanged; 

另外,声明引发事件的方法:
$ b $如果(DataChanged!= null)
DataChanged(this,EventArgs.Empty); b

  protected void OnDataChanged()
{

}

然后,在 OnProgressChanged OnDataChanged 方法。



现在你需要做的是:


  1. 使用类似于 form1.DataChanged + = new EventHandler ....的形式将Form2附加到Form1中的事件。

  2. 在该事件处理程序中,更新Form2中的控件

Form1的当前实例创建时的Form2构造函数,并在Form2的构造函数中附加事件处理函数。还有其他的选择。



编辑2

考虑一下:为什么不把投票编码到一个单独的类中,只要数据发生变化就会提供一个事件然后,您可以将Form1和Form2都附加到这些事件中。这将使Form2与Form1无关。


I Have one Form (LoginForm) which has a Background Worker Monitoring a database for new entries.

Then I have another Form (AdminForm) which I need to signal to update its datagrids whenever new data is available.

I could poll the database in the AdminForm too, but considering that LoginForm is already doing some polling, which can be costly, I just want to signal AdminForm to update the DataGridViews with the new data.

You may ask, why is LoginForm doing the polling when youre showing the stuff in the AdminForm? Well, LoginForm is actually processing the data and sending it over the Serial Port :$ heheh. I want it to be able to process the data without having an administrator logged in all the time.

解决方案

You could have your background worker signal progress (WorkerReportsProgress property set to true, then calling ReportProgress).

In that event handler (OnProgressChanged), invoke another public event you create that signals receivers that data should be updated. Your admin form could subscribe to that event.

As I'm not quite sure whether the OnProgressChanged event is invoked in the context of the main thread or in the context of the background worker's thread, I'd suggest you use this.Invoke when actually doing the UI update.

EDIT
Using OnProgressChanged and another event, do the following:

In the Form1 class, declare a new event like

public event EventHandler DataChanged;

Also, declare a method that raises the event:

protected void OnDataChanged()
{
    if (DataChanged != null)
        DataChanged(this, EventArgs.Empty);
}

Then, in the OnProgressChanged method, call your OnDataChanged method.

All you need to do now is:

  1. Attach Form2 to the event in Form1 using something like form1.DataChanged += new EventHandler....
  2. In that event handler, update your controls in Form2

You might for example pass the current instance of Form1 to the constructor of Form2 when it is created and attach the event handler in the constructor of Form2. There are other options as well.

EDIT 2
Thinking about it: Why don't you put the polling code into a separate class that provides an event whenever data changes. Then you can attach both Form1 and Form2 to these events. This would make Form2 independant from Form1.

这篇关于使用BackGroundWorker从另一个窗体更新DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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