Skip to main content

Overview

Virtual Mode allows you to supply grid data through events without storing it in memory. This is perfect for:
  • Very large datasets that don’t fit in memory
  • Data from external sources (files, network, databases)
  • Calculated or generated data
  • Maximum performance with minimal memory usage

Basic Virtual Mode

Setting Up Virtual Data

OnGetValue Event

Called when the grid needs to display a cell:

OnSetValue Event

Called when a cell is edited:

Complete Example

From Unit_VirtualMode.pas:

Performance Optimization

Default Column Width

Caching Strategies

For expensive data retrieval, implement caching:

Custom Cell Formatting

Format Individual Cells

Format Entire Rows

Real-World Examples

File-Based Data

Database Query

The above database example is inefficient. For database data, use Database Grid instead, which properly uses dataset cursors.

Generated Data

Key Concepts

Virtual Rendering

TeeGrid only calls OnGetValue for visible cells:
  • Scrolling automatically triggers OnGetValue for newly visible cells
  • No need to pre-load all data
  • Memory usage is constant regardless of row count

Event Timing

  • OnGetValue: Called for display, sorting, searching
  • OnSetValue: Called when user edits a cell
Both events are called on-demand, not in advance.

Column Width

If you don’t provide a default width:
The grid will call OnGetValue for all rows to calculate optimal width. For 20,000 rows, this is slow!

Advantages

  1. Memory Efficient: Only visible data is in memory
  2. Fast Startup: No need to load all data upfront
  3. Unlimited Rows: Can handle millions of rows
  4. Flexible: Data can come from anywhere
  5. Real-time: Data can change between calls

Limitations

  1. No Automatic Sorting: You must implement sort logic
  2. No Built-in Search: Implement your own search
  3. Caching Required: For expensive data sources
  4. Width Calculation: Provide default width or it calculates from all rows

Best Practices

  1. Always provide default column width for large datasets
  2. Implement caching for expensive data retrieval
  3. Use for truly large datasets (10,000+ rows)
  4. For databases, use Database Grid instead
  5. Test performance with representative data

When to Use Virtual Mode

Use Virtual Mode When:

  • Dataset doesn’t fit in memory
  • Data is calculated/generated
  • Reading from files or streams
  • Need maximum performance with millions of rows

Use Database Grid When:

  • Data comes from TDataSet
  • Need sorting and filtering
  • Dataset fits in memory
  • Want automatic updates

Use Array/List Binding When:

  • Data is already in memory as collections
  • Need automatic column generation via RTTI
  • Dataset is reasonably sized (<10,000 rows)

Next Steps