Blog
All Blog Posts | Next Post | Previous Post
Next Generation Data Grid for Delphi: Cell Classes
Today
Intro
When working with data grids, one size rarely fits all. Different types of data often require different visual representations and interaction patterns. That's where custom cell classes in TTMSFNCDataGrid come into play. By leveraging the power of custom cell classes, you can create rich, interactive grids that go beyond simple text display to include checkboxes, buttons, progress bars, images, and even hierarchical nodes.
Understanding Cell Classes in TTMSFNCDataGrid
At its core, TTMSFNCDataGrid uses a flexible cell class system that allows different types of content and behavior within the grid. All cell classes inherit from the base TTMSFNCDataGridCell class, which provides fundamental functionality for rendering content and handling user interactions.
The beauty of this system is that the grid can dynamically assign different cell types to different cells, rows, or columns based on your data and requirements. This means you can have a checkbox in one column, a progress bar in another, and buttons in a thirdall within the same grid.
Available Built-in Cell Classes
TTMSFNCDataGrid comes with several pre-built cell classes, each designed for specific use cases:
1. TTMSFNCDataGridCell (Base Class)
The fundamental class that all other cell types inherit from. It handles basic functionality like:
- Rendering cell content
- Managing cell boundaries and positioning
- Handling user interactions
- Storing cell data
2. TTMSFNCDataGridNodeCell
Perfect for creating hierarchical data structures:
- Supports expandable/collapsible nodes
- Creates tree-like structures within the grid
- Manages parent-child relationships
- Includes customizable expand/collapse icons
3. TTMSFNCDataGridCheckBoxCell
Ideal for boolean data or row selection:
- Displays interactive checkboxes
- Supports checked/unchecked states
- Triggers OnChange events
- Perfect for multi-select scenarios
4. TTMSFNCDataGridButtonCell
Enables action buttons within cells:
- Creates clickable buttons
- Supports custom button text
- Triggers OnClick events
- Great for row-specific actions like "Edit" or "Delete"
5. TTMSFNCDataGridProgressBarCell
Visualizes progress or percentage data:
- Displays progress bars
- Supports value ranges (0-100 or custom max)
- Visual representation of completion status
- Ideal for task tracking or status indicators
6. TTMSFNCDataGridBitmapCell
Displays images within grid cells:
- Supports various image formats
- Customizable image alignment
- Perfect for product catalogs or user avatars
- Handles bitmap rendering efficiently
Implementing Custom Cell Classes
The real power comes from dynamically assigning cell classes based on your needs. This is achieved through the OnGetCellClass event, which fires for each cell and allows you to specify which cell class to use.
Basic Implementation
Here's a simple example that assigns different cell classes to different columns:
procedure TForm1.GridGetCellClass(Sender: TObject; ACol, ARow: Integer;
var CellClassType: TTMSFNCDataGridCellClass);
begin
// Column 0: Standard cells
if ACol = 0 then
CellClassType := TTMSFNCDataGridCell
// Column 1: Checkboxes
else if ACol = 1 then
CellClassType := TTMSFNCDataGridCheckBoxCell
// Column 2: Buttons
else if ACol = 2 then
CellClassType := TTMSFNCDataGridButtonCell
// Column 3: Progress bars
else if ACol = 3 then
CellClassType := TTMSFNCDataGridProgressBarCell
// Default to standard cell
else
CellClassType := TTMSFNCDataGridCell;
end;Practical Example: Building a Task Management Grid
Let's create a real-world examplea task management grid that showcases various cell classes:
procedure TForm1.SetupTaskGrid;
begin
// Configure grid
Grid.RowCount := 6; // 1 header + 5 data rows
Grid.ColumnCount := 5;
// Set column headers
Grid.Cells[0, 0] := 'Complete';
Grid.Cells[1, 0] := 'Priority';
Grid.Cells[2, 0] := 'Task Name';
Grid.Cells[3, 0] := 'Progress';
Grid.Cells[4, 0] := 'Action';
// Configure column widths
Grid.ColumnWidths[0] := 70; // Checkbox column
Grid.ColumnWidths[1] := 60; // Priority
Grid.ColumnWidths[2] := 200; // Task name
Grid.ColumnWidths[3] := 100; // Progress bar
Grid.ColumnWidths[4] := 80; // Button
Grid.ColumnWidths[5] := 50; // Icon
// Assign the OnGetCellClass event
Grid.OnGetCellClass := GridGetCellClass;
// Populate sample data
PopulateTaskData;
end;
procedure TForm1.GridGetCellClass(Sender: TObject; ACol, ARow: Integer;
var CellClassType: TTMSFNCDataGridCellClass);
begin
// Skip header row
if ARow = 0 then
begin
CellClassType := TTMSFNCDataGridCell;
Exit;
end;
case ACol of
0: CellClassType := TTMSFNCDataGridCheckBoxCell; // Complete checkbox
3: CellClassType := TTMSFNCDataGridProgressBarCell; // Progress
4: CellClassType := TTMSFNCDataGridButtonCell; // Action button
else
CellClassType := TTMSFNCDataGridCell;
end;
end;
procedure TForm1.PopulateTaskData;
begin
// Task 1
Grid.Booleans[0, 1] := False; // Not complete
Grid.Cells[1, 1] := 'High';
Grid.Cells[2, 1] := 'Implement login system';
Grid.ProgressValue[3, 1] := 75; // 75% progress
Grid.ControlTexts[4, 1] := 'Details';
// Task 2
Grid.Booleans[0, 2] := True; // Complete
Grid.Cells[1, 2] := 'Medium';
Grid.Cells[2, 2] := 'Design database schema';
Grid.ProgressValue[3, 2] := 100; // 100% progress
Grid.ControlTexts[4, 2] := 'Details';
// Continue for other tasks...
end;Working with Cell Class Properties
Once you've assigned cell classes, you can interact with their specific properties and methods:// Set checkbox state
Grid.Booleans[0, 1] := True;
// Handle checkbox changes
procedure TForm1.GridCellCheckBoxChange(Sender: TObject; AColumn, ARow: Integer);
begin
if Grid.Booleans[AColumn, ARow] then
TTMSFNCUtils.Log('Task marked as complete')
else
TTMSFNCUtils.Log('Task marked as incomplete');
end;// Set button text
Grid.ControlTexts[4, 1] := 'Edit';
// Handle button clicks
procedure TForm1.GridCellButtonClick(Sender: TObject; AColumn, ARow: Integer);
begin
TTMSFNCUtils.Log('Button clicked at row ' + IntToStr(ARow));
// Perform action like opening an edit dialog
end;Creating Your Own Cell Classes
Real-World Custom Cell Class Example: Star Rating Cell
For ultimate flexibility, you can create your own cell classes by inheriting from TTMSFNCDataGridCell.Let's create a practical custom cell class that you might actually use in a real application - a star rating cell. This is perfect for product reviews, customer feedback systems, or any scenario where users need to provide or view ratings.
The Star Rating Cell Class
Here's a complete implementation of a custom star rating cell that:
- Displays 1-5 stars
- Shows filled/empty stars based on the rating
Conclusion
Custom cell classes in TTMSFNCDataGrid open up a world of possibilities for creating rich, interactive data grids. By understanding the available cell types and how to implement them effectively, you can transform simple data displays into powerful, user-friendly interfaces.
Whether you're building a task management system with progress indicators and action buttons, a settings panel with checkboxes and dropdowns, or a product catalog with images and ratings, custom cell classes provide the flexibility you need.
Start experimenting with different cell classes in your projects, and you'll quickly discover how they can enhance both the functionality and visual appeal of your applications. The key is to match the right cell class to the right data type and user interaction pattern.
Happy coding, and may your grids be forever dynamic and engaging!
Pieter Scheldeman
Related Blog Posts
-
Next Generation Data Grid for Delphi: Getting Started
-
Next Generation Data Grid for Delphi: Adding, Formatting & Converting Data
-
Next Generation Data Grid for Delphi: Filtering & Sorting
-
Next Generation Data Grid for Delphi: Grouping
-
Next Generation Data Grid for Delphi: Webinar Replay Available!
-
Next Generation Data Grid for Delphi: Cell Controls
-
Next Generation Data Grid for Delphi: Master-Detail
-
Next Generation Data Grid for Delphi: Calculations
-
Next Generation Data Grid for Delphi: Import & Export
-
Next Generation Data Grid for Delphi: Template
-
Next Generation Data Grid for Delphi: Filter Row
-
Next Generation Data Grid for C++: Getting Started
-
Freebie Friday: Next Generation Grid Quick Sample Data
-
Next Generation Data Grid for Delphi: Columns Editor
-
Next Generation Data Grid for Delphi: File Drag & Drop
-
Next Generation Data Grid for Delphi: Visual Grouping
-
Next Generation Data Grid for Delphi: FMX Linux Support
-
Next Generation Data Grid for Delphi: Header & Footer Buttons
-
Next Generation Data Grid for Delphi: Paging
-
Next Generation Data Grid for Delphi: Cell Classes
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post