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

问题描述

我正在寻找展示如何使用数据网格视图显示存储在业务对象中的快速变化数据的教程或示例.下面是一个例子:假设我有以下课程:

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.

屏幕只需要每 250 毫秒显示一次更新.
我还想检查哪些单元格发生了变化.我想知道哪些单元格发生了变化,以便特定单元格在短时间内获得新颜色.例如,如果第 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:

一个.创建项目数据源 请参阅: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.

另外两件事:

  1. 支持嘿看!"颜色变化功能,您可能需要深入挖掘.本质上,表单中的代码应该监视列表中每个项目的 PropertyChanged 事件.当事件引发时,您将需要找到 .DataBoundItem 属性等于您的业务对象上更改的属性的 DataGridViewRow,然后找到单元格绑定到更改的属性.一旦找到要闪烁"的单元格,就可以将该单元格添加到要临​​时突出显示的单元格列表中,相应地更改 CellStyle.最后,您需要一个计时器来定期清除或修剪此列表,将 CellStyle 恢复为原始样式.
  2. 如果项目在后台线程上更新,您可能会遇到 BindingList线程问题.您可以通过将 BindingList 替换为 ThreadedBindingList 来解决此问题.请参阅:您如何正确更新来自后台线程的数据绑定 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.

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

08-18 14:13