Real-Time DataGridView?

前端 未结 1 1872
旧巷少年郎
旧巷少年郎 2021-01-03 10:36

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

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 10:55

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

    0 讨论(0)
提交回复
热议问题