oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Just-In-Time Data Loading For DataGrids
Pages: 1, 2, 3, 4, 5, 6

Testing the JIT approach

To test the JIT approach, we'd like to know which row is being displayed and when the cache has to go back to the dataReader for data. To do so, you'll add one line to the CellValueNeeded handler:

lblMsg.Text = "Row " + String.Format( "{0:N0}", e.RowIndex ) + " of " + String.Format( "{0:N0}", this.dataGridView1.RowCount );

That will show you the row, but finding out when the cache asks for data is a bit trickier. The cleanest way to do so is to create an event for the Cache class:

public delegate void RetrievedDataHandler( object dataSource, RetrievingDataEventArgs e );
public event RetrievedDataHandler OnDataRetrieved;

This event uses the RetrievingDataEventArgs class that you will also define:

public class RetrievingDataEventArgs : EventArgs
   public readonly int rowID;
   public RetrievingDataEventArgs( int rowID )
      this.rowID = rowID;

When the Cache has to ask for data, it fires the event:

 private string RetrieveData_CacheIt_ThenReturnElement(
     int rowIndex, int columnIndex )
    // fire the event indicating that we had to get data for the cache
    if ( OnDataRetrieved != null )
       RetrievingDataEventArgs args = new RetrievingDataEventArgs( rowIndex );
       OnDataRetrieved( this, args );

The JITData class can register to receive the event just after it creates its Cache:

memoryCache = new Cache( dr, PageSize );
memoryCache.OnDataRetrieved += new Cache.RetrievedDataHandler( memoryCache_OnDataRetrieved );

To keep track of these events, add a new label next to the radio buttons, named lblCacheMsg and set its text to "Using Cache." The event handler will fill this label with the information about which row triggered the event:

void memoryCache_OnDataRetrieved( object dataSource, Cache.RetrievingDataEventArgs e )
   lblMsg.Text = "Retrieved data for row " + e.rowID;

Each time data is retrieved from the database the event is fired, and the label is updated, as shown in Figure 2.

Catching the Event
Figure 2. Catching the event

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow