实时数据网格视图?
我正在寻找教程或示例,展示如何使用数据网格视图来显示存储在业务对象中的快速变化的数据。这是一个例子: 假设我有以下类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了支持此功能,您应该使用 WinForms 的数据绑定功能自动为您完成大部分工作。
如果您还没有这样做,您应该使用
BindingSource
来使用设计器将网格中的列绑定到业务对象。简而言之:a. 创建项目数据源请参阅:http://www.telerik.com/help/openaccess-orm/openaccess-tasks-howto-object-data-source.html
b. 将 DataGridView 的 DataSource 属性设置为该数据源。这将自动在您的表单上(位于设计器的下托盘中)创建一个
BindingSource
。它还会自动为每个属性创建一个列,然后您可以根据需要对其进行修改。您的业务对象类应实现
INotifyPropertyChanged
。有关如何执行此操作的示例,请参阅 http://msdn.microsoft。 com/en-us/library/ms743695.aspx。通过这样做,当后台线程更改业务对象时,DataGridView 将自动更新单元格。.DataSource
属性设置为BindingList
。在您的代码中,您应该在此 BindingList 中添加和删除项目。通过这样做,DataGridView 将在列表更新时自动在网格中添加和删除行。还有两件事:
PropertyChanged
事件。引发事件时,您需要找到DataGridViewRow
,其.DataBoundItem
属性等于业务对象上更改的属性,然后找到绑定到更改的属性。一旦找到要“闪烁”的单元格,您就可以将该单元格添加到要临时突出显示的单元格列表中,并相应地更改CellStyle
。最后,您将需要一个计时器来定期清除或修剪此列表,将 CellStyle 恢复为原始样式。BindingList
的线程问题。您可以通过用ThreadedBindingList
替换 BindingList 来解决此问题。请参阅:如何正确更新来自后台线程的数据绑定 datagridview。To support this functionality, you should use the databinding features of WinForms to do most of this for you automatically.
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.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..DataSource
property of the BindingSource to aBindingList<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:
PropertyChanged
event for each item in the list. When the event is raised, you will then need to find theDataGridViewRow
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 theCellStyle
accordingly. Lastly, you will need a timer that periodically clears our or trims this list, restoring the CellStyle to the original style.BindingList
. You can get around this by replacing the BindingList with aThreadedBindingList
. See: How do you correctly update a databound datagridview from a background thread.