实时数据网格视图?

发布于 2024-12-11 11:54:15 字数 686 浏览 1 评论 0原文

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

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 字段。这些更新可能每隔几毫秒发生一次。

屏幕实际上只需要每 250 毫秒显示一次更新。
我还想检查哪些单元格发生了变化。 我想知道哪些单元格发生了变化,以便特定单元格在一段时间内获得新的颜色。 例如,如果第 2 行第 5 列中的数据已更改,则该单元格的颜色会更改几秒钟,并且任何其他更改的单元格的颜色也相同。 这基本上是一个实时应用程序,用于显示发生的更改。
多谢

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;
}

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2024-12-18 11:54:15

为了支持此功能,您应该使用 WinForms 的数据绑定功能自动为您完成大部分工作。

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

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

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

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

  3. 将 BindingSource 的 .DataSource 属性设置为 BindingList。在您的代码中,您应该在此 BindingList 中添加和删除项目。通过这样做,DataGridView 将在列表更新时自动在网格中添加和删除行。

还有两件事:

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

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

  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. Create a Project Data Source See: http://www.telerik.com/help/openaccess-orm/openaccess-tasks-howto-object-data-source.html

    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.

  2. 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.

  3. Set the .DataSource property of the BindingSource to a BindingList<YourBusinessObject>. In your code, you should add and remove items from this BindingList. By doing so, the DataGridView will automatically add and remove rows from the grid as the list is updated.

Two more things:

  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文