Click here to Skip to main content
13,861,006 members
Click here to Skip to main content
Add your own
alternative version


194 bookmarked
Posted 29 Sep 2007

Real-Time Data Grid

, 29 Sep 2007
Rate this:
Please Sign up or sign in to vote.
An Excel-style grid displaying data changes in real-time
Screenshot - ActiveGrid.gif


This article provides a surprisingly efficient way of displaying real-time data changes in an Excel-style grid. When choosing to display data in a grid pattern, your natural first-choice would be to use a DataGrid control bound to a DataSet. However, I thought I'd try using a ListView control for the same purpose.

The aim was to provide a lightweight control that requires very little memory and processing power and has very small latency under heavy load. The simple definition of latency is the time delay between the moment something is initiated and the moment its first effect begins. For software this is usually measured as the time between the raw data arriving and the results being displayed on the screen.


Q. Why does it matter how much processing power is being used?

A. Developing software for the financial sector isn't for the faint-hearted. Huge sums of money can be made or lost in a very short space of time and for the developer, this means that the software has to be reliable, robust and above all, very fast. One of the hallmarks of well-written financial software is very small latency.

The main cause of problems is due to the sheer volume of data that needs to be processed and analysed as quickly as possible; a corporate profit warning, an unexpected change in interest rates or a terrorist attack will all send a shockwave around the financial markets of the world and result in an overwhelming amount of data being generated. This is known in the trade as a 'Fast-Market'.

The data is usually received via data-streams from financial exchanges like Dow-Jones, Liffe and Eurex and this is an obvious case for multi-threaded processing being used to boost performance. However, the data also needs to be displayed in an informative way and this requires the use of a GUI - a notoriously single-threaded process. In poorly designed systems this can lead to a fatal bottleneck with the actual rendering of the data taking up far too much of the available processing power. The result will be out-of-date information being displayed to the user as current data is backed-up awaiting display.

If more processing power is being used by the GUI, it will mean that there's less processor available for the data analysis algorithms that are busy crunching data in the background.

The Example Application

The example application is a bit contrived but demonstrates a typical scenario for which a real-time data grid is required. A random data feeder is used to simulate market data arriving on multiple threads. The speed at which the data arrives can be controlled by using a sliding scale from 0 (No-Data) to 10 (Fast-Market). There are also a couple of buttons that will provide a sudden burst of data that will flood the grid with update events all at once, something that is quite common in the financial markets whenever key data is released.

The remaining controls are all concerned with the appearance of the grid itself and are meant to illustrate the effect on performance that the different painting effects have.

Design Overview

This article assumes a working knowledge of .NET programming with C# and a familiarity with the standard windows ListView control. The control of interest is unimaginatively named ActiveGrid and is little more than an owner-drawn ListView control with the View property hardwired to Details.

The ListView control, along with its Items and SubItems, have all been sub-classed so that some of the key functionality can be either intercepted or overridden. A more obvious candidate would be the DataGrid control but for my money, the ListView has always looked more appealing than a DataGrid from an aesthetic point-of-view and this was my main reason for using it. It comes with some very useful built-in functionality and it would be a shame to lose this.

Main Classes

System.Windows.Forms Base ClassDerived Class

ActiveColumnHeader Class

This class is responsible for the presentation of content in both the column header and all of the cells belonging to the column. In addition to all of the existing properties of the System.Windows.Forms.ColumnHeader, a new category of properties has been created to handle the appearance of the cells:

// Category: Cell Appearance
// Horizontal alignment of the text in all cells belonging to this column
Boolean CellHorizontalAlignment;

// Vertical alignment of the text in all cells belonging to this column
StringAlignment CellVerticalAlignment; 

// Format specifier for the text in all cells belonging to this column
String CellFormat;

// Show or hide the value of zero
Boolean DisplayZeroValues;

// Allows the rows to be sorted based on the contents of the cells belonging to this column
SortOrderEnum SortOrder;

ActiveGrid Class

This class is the main control that hosts the collections of both columns and rows. The System.Windows.Forms.ListView class from which it is derived can be displayed in five different view-types but only the Details setting makes any sense if you want to display a grid style view. Consequently, this property is hardwired in the derived class. Additional key properties are provided in two new categories:

// Category: Appearance Alternating Row
// Draw alternating backgrounds
Boolean UseAlternateRowColors;

// Background color of alternate rows in the list
Color AlternatingBackColor;

// Draw backgrounds using a gradient
Boolean UseGradient;

// Direction of the background gradient
LinearGradientMode LinearGradientMode;

// Start Color of the alternating background gradient
Color AlternatingGradientStartColor;

// End Color of the alternating background gradient
Color AlternatingGradientEndColor;

// Category: Flash Behavior
// Flash the cell when its contents are changed
Boolean AllowFlashing;

// Draw gradient backgrounds for flashed cells
Boolean UseFlashGradient;

// Fade-out effect for flashed cells
Boolean UseFlashFadeOut;

// Length of time in milliseconds that a cell remains in the flashed state
Int32 FlashDuration;

// Colour of the text when a cell is in the flashed state
Color FlashForeColor;

// Colour of the background when a cell is in the flashed state
Color FlashBackColor;

// Font used when a cell is in the flashed state
Font FlashFont;

// Start Colour of the background gradient when a cell is in the flashed state
Color FlashGradientStartColor;

// End Colour of the background gradient when a cell is in the flashed state
Color FlashGradientEndColor;

// Direction of the Background gradient for flashed cells
LinearGradientMode FlashLinearGradientMode;

ActiveRow.ActiveCell Class

This class is where most of the new functionality is to be found and mainly concerns the presentation of the cell contents. There are three text regions in every cell:

Screenshot - CellText.gif

The centre text-string is aligned according to the CellHorizontalAlignment setting in the column header. Each of the three text regions has its own user-defined colour, font and string. All aspects of the cell's appearance can be modified at run-time immediately prior to re-painting it. This enables individual cells to be color-coded according to the type of change being made. For example, if the value is increasing it can be drawn in blue, whereas if the value is decreasing it can be drawn in red.

Other Functionality

  • Columns can be moved at run-time
  • Rows can be sorted by column by clicking on the column header
  • The row header behaves like a LinkLabel


What is presented here is an alternative approach to the standard DataGrid that could easily be used under the right circumstances. It's very efficient and can be easily extended to suit your own business requirements.

It also emphasises the relationship between GUI complexity and processor requirements. There is an ever increasing tendency to make software that is as good-looking as possible but unfortunately all of this 'sex-appeal' comes at a price. As a general rule, the more plain and boring your user interface, the better your performance will be. This is important to bear in mind if the processing power could be better used elsewhere. Of course, this can always be offset by better hardware. A top-of-the-range processor and graphics card will serve you well in this example, which is all very well if your budget can run to it. If it can't, you'll just have to rely on good, old-fashioned coding that gets the job done as efficiently as possible.


  • 29 September 2007: Release of ActiveGrid


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jason Mason
Web Developer
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionDisable Left Column Pin
Ali Al-Masry17-Jan-18 4:07
memberAli Al-Masry17-Jan-18 4:07 
QuestionCan you add same codes for VB.NET as well? Pin
Member 1024974119-Sep-17 18:26
memberMember 1024974119-Sep-17 18:26 
GeneralMy vote of 5 Pin
stixoffire26-Aug-15 12:27
memberstixoffire26-Aug-15 12:27 
QuestionOcasional crash when closing Form Pin
oxoocoffee17-Apr-13 7:25
memberoxoocoffee17-Apr-13 7:25 
GeneralMy vote of 5 Pin
noreply-thephoenixprod2-May-11 23:44
membernoreply-thephoenixprod2-May-11 23:44 
GeneralMy vote of 5 Pin
Ha Vinh Hai21-Apr-11 16:02
memberHa Vinh Hai21-Apr-11 16:02 
GeneralMy vote of 5 Pin
milsnips31-Jan-11 19:06
membermilsnips31-Jan-11 19:06 
GeneralGreat Job Pin
Apostol Bakalov9-Mar-10 3:25
memberApostol Bakalov9-Mar-10 3:25 
QuestionWhat makes this lightweight? Pin
FatCatProgrammer24-Nov-09 9:42
memberFatCatProgrammer24-Nov-09 9:42 
GeneralSpeed Issue Pin
smart_dummies21-Mar-09 6:52
membersmart_dummies21-Mar-09 6:52 
GeneralClick event on a cell Pin
ivanf112-Aug-08 11:30
memberivanf112-Aug-08 11:30 
GeneralBackColor [modified] Pin
ivanf111-Aug-08 7:52
memberivanf111-Aug-08 7:52 
QuestionDisplaying real time data using Grid ( C# , winforms) Pin
BAIJUMAX5-Jun-08 23:22
professionalBAIJUMAX5-Jun-08 23:22 
AnswerRe: Displaying real time data using Grid ( C# , winforms) Pin
comm29-Jul-08 4:21
membercomm29-Jul-08 4:21 
GeneralGreat work, but a little bug Pin
langeru21-Apr-08 7:29
professionallangeru21-Apr-08 7:29 
QuestionHow to fix the columnheader's height Pin
namgniht24-Mar-08 1:17
membernamgniht24-Mar-08 1:17 
QuestionBackColor Question Pin
Tom Williamson17-Mar-08 0:55
memberTom Williamson17-Mar-08 0:55 
GeneralSmall Note... Pin
Member 193287613-Mar-08 4:00
memberMember 193287613-Mar-08 4:00 
GeneralRight To Left Pin
Member 442034423-Jan-08 3:50
memberMember 442034423-Jan-08 3:50 
QuestionNice control, but optimal ? Pin
bsargos7-Dec-07 13:31
memberbsargos7-Dec-07 13:31 
GeneralRe: Nice control, but optimal ? Pin
Jason Mason9-Dec-07 2:13
memberJason Mason9-Dec-07 2:13 
QuestionNice control, but optimal ? Pin
bsargos7-Dec-07 13:30
memberbsargos7-Dec-07 13:30 
GeneralSKACERO Pin
jasper01827-Nov-07 4:59
memberjasper01827-Nov-07 4:59 
GeneralRe: SKACERO Pin
Jason Mason28-Nov-07 8:08
memberJason Mason28-Nov-07 8:08 
QuestionHow to save everybody some time and effort Pin
msorens3-Oct-07 10:41
membermsorens3-Oct-07 10:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 29 Sep 2007
Article Copyright 2007 by Jason Mason
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid