YOUSEFUL / DELPHI / COMPONENTS
TAtlasGrid
A grid that shows a TAtlasTable, sorts by header click and edits cells in place with validation.
Description
TAtlasGrid (unit Atlas.Grid) shows a TAtlasTable and edits it in place. Assign Table: the grid registers as a listener and, after every table change, repaints, resizes its row count and resets the column widths from TAtlasColumn.Width scaled to the monitor DPI (widths the user dragged are lost then). Grid row 0 is the header, so grid row n shows view row n - 1. An empty table shows one blank row that cannot be edited. Only visible cells are drawn.
Display. The header shows the column names in bold white on HeaderColor, with ^ or v after the sorted column. Odd grid rows (the first, third, ... data rows) use AlternateColor, the others Color (white). Number columns are right-aligned; all text is one line and ellipsized. The grid publishes only the members listed here: options, row height and owner drawing are fixed. TAtlasThemer sets HeaderColor, AlternateColor, Color and Font.Color.
Sorting. A left click on a header commits a pending edit and sorts by that column, ascending first and then toggling. No click removes the sort; call Table.Sort(-1).
Editing. Typing on an editable cell opens the in-place editor (read-only columns open none). The text typed is a pending edit: the grid validates it and writes it to the table when the cursor moves to another cell (arrow keys, Tab, mouse), when a header is clicked, when focus leaves the grid, on Enter while the editor is closed, or when code calls CommitEdit. An invalid value raises EAtlasDataError from that action: the application's exception handler shows the message, the table keeps its value and the cursor stays. A table change made by code while the editor is closed discards the pending edit, so it is never written into a row that moved.
Limitations of this baseline (checked against the VCL grid code with simulated keystrokes):
- The first Enter only closes the editor; the cell shows its old value until the edit is committed by one of the actions above.
- Escape while the editor is open closes it but keeps the text pending, and the next move stores it. A second Escape discards it.
- A table change made by code while the editor is still open (a
SortorSetFilterfrom a timer, or from a shortcut that does not take the focus) keeps the typed text pending and later stores it into whichever row is under the cursor after the change. CallCommitEditbefore such changes.
The example Make Enter and Escape act at once removes the first two with a form-level key handler; the GridQuickStart sample uses it.
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates a 640 x 320 grid with one fixed header row, no fixed column, 28-pixel rows, grid lines, column sizing, editing, Tab navigation, thumb tracking, a white background and double buffering. |
Destroy | destructor | Detaches from the table, then destroys the grid. |
CommitEdit | method | Validates the pending edit, writes it to the table cell under the cursor and closes the editor. |
CancelEdit | method | Discards the pending edit and closes the in-place editor. |
Col | property | The current column, the same index as the table column. |
Row | property | The current grid row. |
EditorMode | property | True while the in-place editor is open. |
Table | property | The table to show and edit, or nil. |
HeaderColor | property | Background of the header row; the header text is white and bold. |
AlternateColor | property | Background of odd grid rows (the first, third, fifth ... |
Align | property | Standard VCL property, published by this class. |
Anchors | property | Standard VCL property, published by this class. |
Color | property | Standard VCL property, published by this class. |
Font | property | Standard VCL property, published by this class. |
ParentFont | property | Standard VCL property, published by this class. |
TabOrder | property | Standard VCL property, published by this class. |
TabStop | property | Standard VCL property, published by this class. |
Enabled | property | Standard VCL property, published by this class. |
Visible | property | Standard VCL property, published by this class. |
PopupMenu | property | Standard VCL property, published by this class. |
OnClick | event | Standard VCL event, published by this class. |
OnDblClick | event | Standard VCL event, published by this class. |
OnEnter | event | Standard VCL event, published by this class. |
OnExit | event | Standard VCL event, published by this class. |
OnKeyDown | event | Standard VCL event. |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates a 640 x 320 grid with one fixed header row, no fixed column, 28-pixel rows, grid lines, column sizing, editing, Tab navigation, thumb tracking, a white background and double buffering.
Destructors
Destroy destructor
destructor Destroy; override;Detaches from the table, then destroys the grid.
Properties
Col property
property Col;The current column, the same index as the table column. Setting it moves the cursor and commits a pending edit first.
Row property
property Row;The current grid row. Row 0 is the header, so the table view row is Row - 1; check (Row >= 1) and (Row <= Table.RowCount) before using it. Setting it moves the cursor and commits a pending edit first, raising EAtlasDataError if that edit is invalid.
if (Grid.Row >= 1) and (Grid.Row <= Grid.Table.RowCount) then
Grid.Table.DeleteRow(Grid.Row - 1);EditorMode property
property EditorMode;True while the in-place editor is open. Setting it to False closes the editor and keeps its text as the pending edit; call CommitEdit next to store it at once.
Table property
property Table: TAtlasTable read FTable write SetTable;The table to show and edit, or nil. The grid follows the table through a listener and a free notification: freeing the table sets Table to nil. Assigning another table cancels a pending edit.
HeaderColor property
property HeaderColor: TColor read FHeaderColor write SetHeaderColor default $003D2C1E;Background of the header row; the header text is white and bold. Default $003D2C1E, a dark blue.
AlternateColor property
property AlternateColor: TColor read FAlternateColor write SetAlternateColor default $00F8F5F2;Background of odd grid rows (the first, third, fifth ... data rows). Default $00F8F5F2, a light grey.
Align property
property Align;Standard VCL property, published by this class.
Anchors property
property Anchors;Standard VCL property, published by this class.
Color property
property Color;Standard VCL property, published by this class.
Font property
property Font;Standard VCL property, published by this class.
ParentFont property
property ParentFont;Standard VCL property, published by this class.
TabOrder property
property TabOrder;Standard VCL property, published by this class.
TabStop property
property TabStop;Standard VCL property, published by this class.
Enabled property
property Enabled;Standard VCL property, published by this class.
Visible property
property Visible;Standard VCL property, published by this class.
Events
OnClick event
property OnClick;Standard VCL event, published by this class.
OnDblClick event
property OnDblClick;Standard VCL event, published by this class.
OnEnter event
property OnEnter;Standard VCL event, published by this class.
OnExit event
property OnExit;Standard VCL event, published by this class.
OnKeyDown event
property OnKeyDown;Standard VCL event. It also fires for keys pressed in the in-place editor, except Escape, which the grid handles itself.
Methods
CommitEdit method
procedure CommitEdit;Validates the pending edit, writes it to the table cell under the cursor and closes the editor. Does nothing when there is no pending edit. Raises EAtlasDataError when the value does not fit the column; the edit then stays pending. Call it before your own code sorts, filters or changes the table.
Grid.CommitEdit;
Grid.Table.Sort(2, False);CancelEdit method
procedure CancelEdit;Discards the pending edit and closes the in-place editor. If the editor was open, closing it hands its text back as a new pending edit, so to discard what is being typed call CancelEdit twice. The grid calls it itself on every table change.
Grid.CancelEdit;
Grid.CancelEdit; // the editor was openExamples
Show a table and sort it from code
The grid follows the table; commit a pending edit before code moves rows. Prints ascending: b a, view row 0 and descending: a b in the example run.
uses Vcl.Forms, Vcl.Controls, Atlas.Data, Atlas.Grid;
function ShowTable(Form: TForm; Table: TAtlasTable): TAtlasGrid;
begin
Result := TAtlasGrid.Create(Form);
Result.Parent := Form;
Result.Align := alClient;
Result.HeaderColor := $00503A28;
Result.AlternateColor := $00F4F1EE;
Result.Table := Table; // the grid now follows every change of the table
end;
procedure SortByColumn(Grid: TAtlasGrid; Column: Integer);
begin
Grid.CommitEdit; // store a pending edit before the rows move
Grid.Table.Sort(Column, (Grid.Table.SortColumn <> Column) or not Grid.Table.Ascending);
end;
function CurrentViewRow(Grid: TAtlasGrid): Integer;
begin
// Grid row 0 is the header; -1 when the cursor is not on a data row.
if (Grid.Row >= 1) and (Grid.Row <= Grid.Table.RowCount) then
Result := Grid.Row - 1
else
Result := -1;
end;Make Enter and Escape act at once
A form-level key handler (the form sees keys first because KeyPreview is True) that makes Enter store the value immediately and Escape really discard it. Verified with simulated keystrokes: Enter commits, an invalid value raises on Enter, Escape restores the old value, and arrow keys still commit.
uses Winapi.Windows, System.Classes, Vcl.Forms, Vcl.Controls, Atlas.Grid;
type
TGridForm = class(TForm)
public
Grid: TAtlasGrid;
constructor Create(AOwner: TComponent); override;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
end;
constructor TGridForm.Create(AOwner: TComponent);
begin
inherited CreateNew(AOwner);
KeyPreview := True; // the form sees keys before the grid's in-place editor
OnKeyDown := FormKeyDown;
Grid := TAtlasGrid.Create(Self);
Grid.Parent := Self;
Grid.Align := alClient;
end;
procedure TGridForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (ActiveControl <> Grid) or not Grid.EditorMode then
Exit;
if Key = VK_RETURN then
begin
Grid.EditorMode := False; // hand the editor text to the grid
Grid.CommitEdit; // validate and store it now, or raise EAtlasDataError
end
else if Key = VK_ESCAPE then
begin
Grid.CancelEdit; // closing the editor keeps its text pending...
Grid.CancelEdit; // ...so the second call discards it
Key := 0;
end;
end;Related
TAtlasTable · EAtlasDataError · TAtlasValueEdit · TAtlasPivot
Also in Atlas VCL
TAtlasTable · TAtlasValueEdit · TAtlasTree · TAtlasWorkbook · TAtlasSheetGrid · TAtlasPivot · TAtlasInspector · TAtlasRichEdit · TAtlasSpellChecker · TAtlasChart · TAtlasGauge · TAtlasThemer · TAtlasWizard · TAtlasWorkspace · TAtlasCommandBar · TAtlasActionTile · TAtlasTileBoard · TAtlasNavigation · TAtlasMap · TAtlasGraph · TAtlasDiagram · TAtlasPrintDocument · TAtlasSchedule · TAtlasScheduler · TAtlasTaskPlan · TAtlasGantt · TAtlasPdfDocument · TAtlasPdfViewer