简体   繁体   English

实时DataGridView?

[英]Real-Time 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;
}

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. 现在,我有一个在gui线程之外运行的线程,该线程正在接收头寸和价格更新,并更新CurrentPrice,CurrentValue和TotalValue字段。 These updates can occur every couple of milliseconds. 这些更新可能每隔几毫秒发生一次。

The screen only really needs to show updates every 250ms. 屏幕实际上只需要每250毫秒显示一次更新。
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. 例如,如果第5列第2行中的数据已更改,则该单元格会更改颜色几秒钟,而其他所有更改的单元格都会更改颜色。 This is basically a real time application to show the changes as they happen. 这基本上是一个实时应用程序,用于显示发生的更改。
Thanks a lot 非常感谢

To support this functionality, you should use the databinding features of WinForms to do most of this for you automatically. 为了支持此功能,您应该使用WinForms的数据绑定功能自动为您完成大部分操作。

  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. 如果还没有,则应该使用BindingSource来使用设计器将网格中的列绑定到业务对象。 In short: 简而言之:

    a. 一种。 Create a Project Data Source See: http://www.telerik.com/help/openaccess-orm/openaccess-tasks-howto-object-data-source.html 创建项目数据源,请参见: 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. DataGridView 的DataSource属性设置为该数据源。 This will automatically create a BindingSource on your form (in the lower tray of the designer). 这将在您的表单上(在设计器的下部托盘中)自动创建一个BindingSource 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 . 您的业​​务对象类应实现INotifyPropertyChanged For an example on how to do that, see http://msdn.microsoft.com/en-us/library/ms743695.aspx . 有关如何执行此操作的示例,请参见http://msdn.microsoft.com/zh-cn/library/ms743695.aspx By doing so, the DataGridView will automatically update cells as the business objects are changed by the background thread. 这样,随着后台线程更改业务对象,DataGridView将自动更新单元。

  3. Set the .DataSource property of the BindingSource to a BindingList<YourBusinessObject> . 将BindingSource的.DataSource属性设置为BindingList<YourBusinessObject> In your code, you should add and remove items from this BindingList. 在您的代码中,应该从此BindingList中添加和删除项目。 By doing so, the DataGridView will automatically add and remove rows from the grid as the list is updated. 这样,DataGridView将在列表更新时自动在网格中添加和删除行。

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. 本质上,表单中的代码应监视列表中每个项目的PropertyChanged事件。 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. 引发事件时,您将需要找到其.DataBoundItem属性等于在业务对象上更改的属性的DataGridViewRow ,然后找到绑定到更改后的属性的单元格。 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. 一旦找到要“闪烁”的单元格,就可以将该单元格添加到要临​​时突出显示的单元格列表中,从而相应地更改CellStyle Lastly, you will need a timer that periodically clears our or trims this list, restoring the CellStyle to the original style. 最后,您将需要一个计时器,以定期清除或修剪此列表,从而将CellStyle恢复为原始样式。
  2. If items are being updated on the background thread, you may run into threading issues with the BindingList . 如果正在后台线程上更新项目,则BindingList可能会遇到线程问题 You can get around this by replacing the BindingList with a ThreadedBindingList . 您可以通过将BindingList替换为ThreadedBindingList来解决此问题。 See: How do you correctly update a databound datagridview from a background thread . 请参阅: 如何从后台线程正确更新数据绑定的datagridview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM