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
FromUnit_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
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
Column Width
If you don’t provide a default width:Advantages
- Memory Efficient: Only visible data is in memory
- Fast Startup: No need to load all data upfront
- Unlimited Rows: Can handle millions of rows
- Flexible: Data can come from anywhere
- Real-time: Data can change between calls
Limitations
- No Automatic Sorting: You must implement sort logic
- No Built-in Search: Implement your own search
- Caching Required: For expensive data sources
- Width Calculation: Provide default width or it calculates from all rows
Best Practices
- Always provide default column width for large datasets
- Implement caching for expensive data retrieval
- Use for truly large datasets (10,000+ rows)
- For databases, use Database Grid instead
- 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
- Learn about Performance optimization
- Explore Database Grid for database data
- See Array Binding for in-memory data
