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