TAdvStringGrid: Ultimate Guide to Features and Usage

Migrating From TStringGrid to TAdvStringGrid — Step-by-StepMigrating a Delphi/VCL application from TStringGrid to TAdvStringGrid (from TMS Software) can bring significant benefits: richer UI features, built‑in editing and formatting, higher performance with large datasets, and more customization options. This guide walks through a practical, step‑by‑step migration process, covering planning, replacing components, updating code, leveraging new features, and testing. Examples assume Delphi XE or later and a working knowledge of the VCL and object pascal.


Why migrate?

  • Richer cell types: built‑in support for combo boxes, checkboxes, images, progress bars, and more.
  • Cell formatting & styles: per‑cell fonts, colors, and multi‑line text with easy management.
  • Virtual mode & performance: efficient handling of very large datasets via events and on‑demand rendering.
  • Sorting & filtering: out‑of‑the‑box features for user interaction and data manipulation.
  • Advanced events & API: more hooks for custom drawing, editing, and behavior.

1. Plan the migration

  1. Inventory grids: list all forms using TStringGrid, noting functionality: in-place editing, custom drawing, sorting, data-binding, exporting, clipboard support.
  2. Prioritize: start with noncritical/simple grids to gain experience. Move to complex ones once comfortable.
  3. Get TMS components licensed/installed: ensure TAdvStringGrid is available in the IDE and at runtime. Verify versions and compatibility with your Delphi version.
  4. Backup & version control: commit current code before changes so you can revert if needed.

2. Replace the visual component

  • Open the form in the Delphi IDE.
  • Remove TStringGrid (or keep as reference) and drop a TAdvStringGrid onto the form. Set Name to match the previous grid if you want minimal code changes, or update code references accordingly.
  • Adjust alignment, anchors, and other layout properties.

Tip: Keep the original TStringGrid on a hidden form or copy it into a separate branch so you can compare behavior while migrating.


3. Basic property mapping

TStringGrid and TAdvStringGrid share common concepts (Rows, ColCount, RowCount, fixed rows/cols), but properties and methods differ.

Common mappings:

  • TStringGrid.Cells[col,row] -> AdvStringGrid.Cellscol,row
  • TStringGrid.ColCount / RowCount -> AdvStringGrid.ColCount / RowCount
  • FixedRows/FixedCols -> FixedRows/FixedCols (same names)
  • DefaultRowHeight/DefaultColWidth -> RowHeight[]/ColWidths[] for per‑row/col control; use DefaultRowHeight for uniform height.

Note: TAdvStringGrid exposes a richer API (Cells, CellsText, CellObjects, RowObjects). Review the TMS documentation for exact signatures.


4. Updating code that accesses Cells and ranges

Most code that reads/writes Cells will work with minimal changes. Example:

Original TStringGrid:

var   s: string; begin   s := StringGrid1.Cells[2, 3];   StringGrid1.Cells[2, 4] := 'Hello'; end; 

TAdvStringGrid uses the same Cells property:

s := AdvStringGrid1.Cells[2, 3]; AdvStringGrid1.Cells[2, 4] := 'Hello'; 

However, consider using Value or Ints/Floats collections for typed access:

  • AdvStringGrid1.Ints[col, row] for integers
  • AdvStringGrid1.Floats[col, row] for floating values

This avoids repeated string parsing and improves performance.


5. Editing modes and editors

TAdvStringGrid supports many in-place editors. Replace TStringGrid’s event-based editors with built-in editors when appropriate.

Examples:

  • To use a checkbox: set Column.CheckBox to True or use AddCheckBox in cell objects.
  • To use a combo box: use Column.ComboList or use the Editor property to set a picklist.
  • For custom editors, handle OnGetEditorType or OnGetEditMask as needed.

Example: make column 2 a checkbox column

AdvStringGrid1.Column[2].CheckBox := True; 

6. Custom drawing and styling

TStringGrid custom drawing uses OnDrawCell. TAdvStringGrid provides more powerful events and style objects.

  • Use OnGetCellColor / OnGetAlignment / OnGetCellFrame for per‑cell styling.
  • Use TAdvStringGridStyles for reusable named styles (font, color, background).
  • For full custom painting, use OnGetCellBitmap or OnBeforeDrawCell/OnAfterDrawCell.

Example: apply a style to a cell

AdvStringGrid1.Styles.Add('Important').Font.Style := [fsBold]; AdvStringGrid1.Colors[3,5] := clYellow; // simple color set AdvStringGrid1.CellProperties[3,5].Style := 'Important'; 

Note: exact property names may vary by version; consult TMS docs if compilation failures occur.


7. Virtual mode and large datasets

For huge datasets, avoid populating every cell. Use virtual mode with events that supply values on demand:

  • Set AdvStringGrid1.Virtual := True (or use OnGetCellData depending on version).
  • Handle OnGetValue/OnGetCellText to provide content based on row/col indexes.
  • Use RowCount as the logical number of rows; the grid requests display values as needed.

Example pattern:

procedure TForm1.AdvStringGrid1GetCellText(Sender: TObject; ACol, ARow: Integer; var AText: string); begin   AText := MyDataSource.GetValue(ACol, ARow); // fetch from dataset or array end; 

This reduces memory and improves responsiveness.


8. Sorting, filtering, and grouping

TAdvStringGrid has built‑in sorting and filtering features.

  • Enable column sorting: set Column.Sort := ssDefault or call SortCol/SortRows programmatically.
  • For custom sort, handle OnCompareCells to implement locale‑aware or numeric sorting.
  • Filtering: use Filtered property and Filter.Text/FilterOptions or implement custom filtering logic and call UpdateFilter.

Example: numeric sort for column 2

AdvStringGrid1.SortCol := 2; AdvStringGrid1.SortRows(2, gtNumeric); 

9. Exporting, clipboard, and printing

TAdvStringGrid includes convenient export and clipboard methods:

  • CopyToClipboard, SaveToCSV, SaveToExcel, SaveToHtml, SaveToPdf (depending on installed TMS modules).
  • For printing, use PrintGrid or integrate with TAdvGridPrinter.

Example:

AdvStringGrid1.SaveToCSV('data.csv', True); // include headers AdvStringGrid1.CopyToClipboard(True, True); // headers and selection 

10. Events mapping — common conversions

Common TStringGrid events and their TAdvStringGrid equivalents:

  • OnDrawCell -> OnAfterDrawCell / OnBeforeDrawCell (more control)
  • OnSelectCell -> OnCanResizeCell / OnSelectCell (check your version)
  • OnSetEditText -> OnSetEditText / OnCellValidate
  • OnGetEditText -> OnGetCellText or virtual mode callbacks

Always inspect the Object Inspector for available events; TAdvStringGrid provides more granular hooks.


11. Handling special features (merging, fixed rows, row heights)

  • Merging: TAdvStringGrid supports cell merging with MergeCells or automatic merging by property settings.
  • Fixed rows/cols behave similarly, but styling and behavior options are richer.
  • Row heights: set RowHeights[Row] for per‑row control; AutoRowHeight or AutoSizeRow can enable dynamic sizing.

Example: merge a range

AdvStringGrid1.MergeCells(1,1,3,1); // merge cols 1..3 in row 1 

12. Troubleshooting common issues

  • Compilation errors: unit names differ. Replace uses of Grids with AdvGrid or other TMS units; add appropriate uses clause entries (e.g., AdvGrid, Grids? check TMS docs).
  • Property/method not found: consult TMS docs for renamed properties (e.g., CellProperties, Column[]).
  • Behavior differences: test editing, focus, and event order; some events fire differently—adjust handlers accordingly.
  • Performance: avoid per‑cell string conversions; use Ints/Floats collections, virtual mode, or CellObjects for heavy datasets.

13. Example: converting a simple data entry grid

TStringGrid setup (conceptual):

  • Fixed row for headers
  • Editable cells, column 2 has numeric validation
  • OnSetEditText validates input and recalculates totals

TAdvStringGrid approach:

  • Create columns and set Column[2].EditorType := etNumber or handle OnValidateCell for numeric checks.
  • Use Column[0].ComboList for picklists.
  • Use virtual mode if the dataset is large.
  • Use built‑in summary/footer rows (if available) or compute totals on data change events.

Code sketch:

// set up column types AdvStringGrid1.Column[0].ComboList := 'ItemA;ItemB;ItemC'; AdvStringGrid1.Column[1].ReadOnly := False; AdvStringGrid1.Column[2].EditorType := etNumber; // validation procedure TForm1.AdvStringGrid1ValidateCell(Sender: TObject; ACol, ARow: Integer; var Accept: Boolean); begin   if ACol = 2 then     Accept := TryStrToFloat(AdvStringGrid1.Cells[ACol, ARow], Dummy); end; 

14. Testing and QA

  • Functional tests: editing, navigation, clipboard, copy/paste, selection, resizing.
  • Data integrity: ensure no string/number conversions break values.
  • Performance: test with realistic dataset sizes and UI actions.
  • Cross‑platform/look: if using different Windows themes or scaling, check layout and fonts.
  • Regression: run existing unit/integration tests; add tests for new behaviors.

15. Rollout strategy

  • Staged deployment: release with feature flag or in minor update.
  • Collect user feedback on behavior differences (keyboard navigation, editing).
  • Keep fallback plan: maintain branch with TStringGrid for quick rollback.

16. Resources

  • TMS Software documentation and demos for TAdvStringGrid (consult for exact property names matching your installed version).
  • Example projects shipped with TMS components.
  • Community forums and StackOverflow for migration patterns and specific coding questions.

Migrating to TAdvStringGrid modernizes grid behavior and opens many UI possibilities. Take it step by step: replace components, map properties and events, adopt virtual mode for large data, and leverage the advanced editors and styling.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *