实时DataGridView? [英] Real-Time DataGridView?

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

问题描述

我正在寻找教程或示例,显示如何使用数据网格视图显示存储在业务对象中的快速更改的数据。这是一个例子:
说我有以下类:

I'm looking for tutorial or example showing how to use a datagrid view to display quickly changing data which is stored in business objects. Here is an example: Say I have the following classes:

public class StockPosition    
{  
   public string Ticker;  
   public double CurrentPrice;
   public double CurrentPosition;
   public double CurrentValue;  
}

public class CustomerPortfolio
{
    public string Name;
    public double TotalValue;
    public IList<StockPosition> StockPositions;
}

现在,我有一个线程正在外面运行的gui线程正在接收位置和价格更新,并更新CurrentPrice,CurrentValue和TotalValue字段。这些更新可能会每隔几毫秒发生一次。

Now, I have a thread which is running outside the gui thread which are receiving position and price updates, and updating the CurrentPrice, CurrentValue and TotalValue fields. These updates can occur every couple of milliseconds.

屏幕只需要显示每250ms的更新。

,而且我想检查哪些单元格已改变。
我想知道哪些单元格已更改,以便特定的单元格获取一个新的颜色一段时间。
例如,如果第5列第2行中的数据已更改,那么该单元格将更改颜色几秒钟,并且与其他更改的单元格相同。
这基本上是一个实时的应用程序来显示发生的变化。

感谢很多

The screen only really needs to show updates every 250ms.
and also I want to check which cells have changed. I would like to know which cell(s) have changed so that particular cell gets a new color for a few moments. For example if the data in column 5, row 2 has changed then that cell changes color for a few seconds and the same for any other changed cells. This is basically a real time application to show the changes as they happen.
Thanks a lot

推荐答案

要支持此功能,您应该使用WinForms的数据绑定功能自动执行此操作。

To support this functionality, you should use the databinding features of WinForms to do most of this for you automatically.


  1. 如果还没有,您应该使用 BindingSource 使用设计器将网格中的列绑定到业务对象。简而言之:

  1. If you aren't already, you should use a BindingSource to use the designer to bind columns in your grid to your business objects. In short:

a。 创建项目数据源请参阅: http://www.telerik.com/help/openaccess-orm/openaccess-tasks-howto-object-data-source.html

b。 将DataGridView的DataSource属性设置为该数据源。这将自动在您的表单(在设计器的下部托盘中)创建一个 BindingSource 。它还将自动为每个属性创建一个列,然后您可以根据需要进行修改。

b. Set the DataSource property of the DataGridView to that data source. This will automatically create a BindingSource on your form (in the lower tray of the designer). It will also automatically create a column for each property, which you can then modify based on your needs.

您的业务对象类应实现 INotifyPropertyChanged 。有关如何执行此操作的示例,请参阅 http://msdn.microsoft.com /en-us/library/ms743695.aspx 。通过这样做,当后台线程更改业务对象时,DataGridView将自动更新单元。

Your business object classes should implement INotifyPropertyChanged. For an example on how to do that, see http://msdn.microsoft.com/en-us/library/ms743695.aspx. By doing so, the DataGridView will automatically update cells as the business objects are changed by the background thread.

还有两件事: p>

Two more things:


  1. 支持嘿看!颜色变化功能,您可能需要深入挖掘。本质上,表单中的代码应该监视列表中每个项目的 PropertyChanged 事件。当事件被提出时,您将需要找到 DataGridViewRow ,其 .DataBoundItem 属性等于属性已更改在您的业务对象上,然后找到绑定到更改属性的单元格。找到要闪烁的单元格后,您可以将该单元格添加到要临​​时突出显示的单元格列表中,从而相应地更改 CellStyle 。最后,您将需要一个定期清除或修剪此列表的计时器,将CellStyle恢复为原始样式。

  2. 如果在后台线程上更新项目,则可能会遇到线程问题 BindingList 。您可以通过使用 ThreadedBindingList 替换BindingList来解决此问题。请参阅:如何正确更新从背景线程数据绑定datagridview

  1. To support the "hey look!" color change functionality, you will probably need to dig a little deeper. Essentially, the code in your form should monitor the PropertyChanged event for each item in the list. When the event is raised, you will then need to find the DataGridViewRow whose .DataBoundItem property is equal to the property changed on your Business Object, and then find the cell which is bound to the changed property. Once you find the cell that is to be "flashed", then you can add that cell to a list of cells that are to be temporarily highlighted, changing the CellStyle accordingly. Lastly, you will need a timer that periodically clears our or trims this list, restoring the CellStyle to the original style.
  2. If items are being updated on the background thread, you may run into threading issues with the BindingList. You can get around this by replacing the BindingList with a ThreadedBindingList. See: How do you correctly update a databound datagridview from a background thread.

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

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