Virtual grids allow you to display large data sources very quickly.
This sample illustrates binding a grid to an external data source so that no data actually resides in the grid. The data is provided to the grid on-demand through an event that allows you to furnish requested data in the handler.
Features
QueryCellInfo, QueryColCount, and QueryRowCount are events to be handled to implement a virtual grid.
They provide the basic information about the number of rows and columns and the values of data.
Data from the data source is set through the QueryCellInfo handler:
void GridQueryCellInfo(object sender, GridQueryCellInfoEventArgs e) { if (e.RowIndex > 0 && e.ColIndex > 0) { e.Style.CellValue = this._extData[e.RowIndex - 1, e.ColIndex - 1]; e.Handled = true; } }
Conditional formatting can be done in the QueryCellInfo handler. Those data that are divisible by three is set with the background color LightPink.
if(this._extData[e.RowIndex - 1, e.ColIndex - 1] % 3 == 0) e.Style.BackColor = Color.LightPink; e.Handled = true;
QueryColCount and QueryRowCount events are used to return column and row count on-demand.
The SaveCellInfo event is used to store data, back into the data source when it is modified.
void GridSaveCellInfo(object sender, GridSaveCellInfoEventArgs e) { this._extData[e.RowIndex - 1, e.ColIndex - 1] = int.Parse(e.Style.CellValue.ToString()); e.Handled = true; }
The grid is purely virtual only when e.Handled is set to true.
e.RowIndex and e.ColIndex start from (-1,-1).
The reason to query with -1 is to denote -1 for colstyles and rowstyles. When the table style is requested, both the e.RowIndex and e.ColIndex will be -1. Therefore, a condition check, if (e.RowIndex > 0 && e.ColIndex > 0), is required in these handlers.
QueryRowHeight and QueryColWidth handlers are used to set the size of the row or column (optional).
The QueryCoveredRange handler (optional) is used to cover specified ranges.
Odd rows are covered from columns 1 to 3 and odd-even row pairs are covered in column 6.
The virtual grid will be refreshed by calling the ResetVolatileData, and all events described above will be triggered for that call.