[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] | Reference for unit 'Grids' (#lcl)

# TStringGrid

[Properties (by Name)] [Methods (by Name)] [Events (by Name)]

Implements a grid specialized for textual content.

## Declaration

Source position: grids.pas line 1835

`type TStringGrid = class(TCustomStringGrid)`
`protected`
`class procedure WSRegisterClass; override;` |  | Registers this component class with the current WidgetSet.
`public`
`property Modified: Boolean;` |  | Indicates whether the content in the grid cells has been changed.
`property InplaceEditor: TWinControl;` |  | Cell editor for the grid control.
`published`
`property Align: TAlign;` |  | Specifies the placement of the control on its Parent control.
`property AlternateColor: TColor;` |  | Color used for the background in alternate rows in the grid.
`property Anchors: TAnchors;` |  | The set of anchor definitions for this control.
`property AutoAdvance: TAutoAdvance;` |  | Direction used when automatically moving the selection to the next selectable cell.
`property AutoEdit: Boolean;` |  | Indicates if edit mode is automatically entered when a cell is selected.
`property AutoFillColumns: Boolean;` |  | Indicates whether columns are automatically resized to fill the visible area in the grid.
`property BiDiMode: TBiDiMode;` |  | Indicates whether text controls use in bi-directional reading.
`property BorderSpacing: TControlBorderSpacing;` |  | Determines the inner and outer border spacing for this control.
`property BorderStyle: TBorderStyle;` |  | Indicates the border style displayed around the control.
`property CellHintPriority: TCellHintPriority;` |  | Identifies how text is combined to form the Hint property.
`property Color: TColor;` |  | The background color for the control.
`property ColCount: Integer;` |  | Number of columns in the grid.
`property ColRowDraggingCursor: TCursor;` |  | Cursor shape displayed when a column or row is dragged in the grid control.
`property ColRowDragIndicatorColor: TColor;` |  | Color used to render the drag cursor when a column or row is dragged in the grid.
`property ColSizingCursor: TCursor;` |  | Cursor shape displayed when a column is resized.
`property ColumnClickSorts: Boolean;` |  | Indicates if clicking a column header changes its sort order.
`property Columns: TGridColumns;` |  | Definitions for the columns displayed the grid.
`property Constraints: TSizeConstraints;` |  | Contains the minimum and maximum Width and Height for the control.
`property DefaultColWidth: Integer;` |  | Default width used for grid columns.
`property DefaultDrawing: Boolean;` |  | Indicates if the default drawing mechanism is used to draw the background and text for cells.
`property DefaultRowHeight: Integer;` |  | Default row height for newly created grid rows.
`property DoubleBuffered: Boolean;` |  | When enabled, it reduces flicker when the control is painted.
`property DragCursor: TCursor;` |  | The cursor shape shown during a drag operation.
`property DragKind: TDragKind;` |  | Indicates the action performed for a drag operation: drag-and-drop or drag-and-dock.
`property DragMode: TDragMode;` |  | Determines how a drag operation is started for the control.
`property Enabled: Boolean;` |  | Determines whether the control responds to mouse or keyboard input.
`property ExtendedSelect: Boolean;` |  | Controls whether extending the current cell selection(s) on the control is enabled.
`property FadeUnfocusedSelection: Boolean;` |  | Indicates if a special color is used for selected cells when the control loses focus.
`property FixedColor: TColor;` |  | The color used for the fixed cells in the grid.
`property FixedCols: Integer;` |  | The number of fixed columns in the grid.
`property FixedRows: Integer;` |  | Number of the fixed rows in the grid.
`property Flat: Boolean;` |  | Indicates if cells are displayed using a Flat appearance.
`property FocusColor: TColor;`
`property FocusRectVisible: Boolean;`
`property Font: TFont;` |  | The font to be used for text display in this control.
`property GridLineColor: TColor;` |  | Color used for the grid lines on the control.
`property GridLineStyle: TPenStyle;` |  | Style used to draw the grid lines on the control.
`property GridLineWidth: Integer;` |  | Width (thickness) for grid lines drawn on the control.
`property HeaderHotZones: TGridZoneSet;` |  | Contains the grid zones which are hot-lighted (hovered) on the control.
`property HeaderPushZones: TGridZoneSet;` |  | Contains the grid zones which are drawn in a "pushed" state on the control.
`property ImageIndexSortAsc: TImageIndex;` |  | Ordinal position for the image used when a column is sorted in ascending order.
`property ImageIndexSortDesc: TImageIndex;` |  | Ordinal position for the image used when a column is sorted in descending order.
`property MouseWheelOption: TMouseWheelOption;` |  | Sets the mouse wheel behavior for the grid control.
`property Options: TGridOptions;` |  | Contains the set of optional features and/or behaviors enabled for the grid control.
`property Options2: TGridOptions2;` |  | Contains additional options for scrolling and editor behavior enabled in the grid control.
`property ParentBiDiMode: Boolean;` |  | Indicates whether the BiDiMode settings in the Parent control are used.
`property ParentColor: Boolean;` |  | Uses the Color from the Parent control when enabled.
`property ParentDoubleBuffered: Boolean;` |  | Value for the DoubleBuffered property in a Parent control.
`property ParentFont: Boolean;` |  | Indicates if the Font from the Parent control is used in the control.
`property ParentShowHint: Boolean;` |  | If True, the value of ShowHint for the control will be the same as the one from the Parent. Default is True.
`property PopupMenu: TPopupMenu;` |  | A context-sensitive menu that pops up when the right mouse button is clicked over this control.
`property RangeSelectMode: TRangeSelectMode;` |  | Controls the range selection mode used for the grid.
`property RowCount: Integer;` |  | Number of rows in the grid.
`property RowSizingCursor: TCursor;` |  | Cursor shape displayed when the row height is resized.
`property ScrollBars: TScrollStyle;` |  | Scrollbars displayed for the grid control.
`property ShowHint: Boolean;` |  | Enables Hint display for the control.
`property TabAdvance: TAutoAdvance;` |  | Controls the behavior for Tab navigation in the grid control.
`property TabOrder: TTabOrder;` |  | Indicates the navigation order for the control when the user presses the Tab or Shift+Tab key.
`property TabStop: Boolean;` |  | Allows the user to navigate to / from the control by pressing the Tab or Shift+Tab keys.
`property TitleFont: TFont;` |  | The font used for text in a column title.
`property TitleImageList: TCustomImageList;` |  | The list with images displayed in column titles.
`property TitleStyle: TTitleStyle;` |  | The drawing style used for the fixed column titles.
`property UseXORFeatures: Boolean;` |  | When True, the dotted focus rectangle is painted using an XOR raster operation.
`property Visible: Boolean;` |  | Allows the control, and all of its children, to be displayed or hidden.
`property OnAfterSelection: TOnSelectEvent;` |  | Event handler signalled after a new Selection is made in the grid.
`property OnBeforeSelection: TOnSelectEvent;` |  | Event handler signalled before changing the Selection in the grid control.
`property OnCellProcess: TCellProcessEvent;` |  | Event handler signalled when copying or pasting content for Cells in the grid.
`property OnChangeBounds: TNotifyEvent;` |  | Event handler signalled when the Bounds for the control have been changed.
`property OnCheckboxToggled: TToggledCheckboxEvent;` |  | Event handler signalled when the value for a check box cell is toggled.
`property OnClick: TNotifyEvent;` |  | Notification handler for mouse clicks.
`property OnColRowDeleted: TGridOperationEvent;` |  | Event handler signalled when a grid column or row is deleted.
`property OnColRowExchanged: TGridOperationEvent;` |  | Event handler signalled when a column or row has been exchanged with another.
`property OnColRowInserted: TGridOperationEvent;` |  | Event handler signalled when a column or row is inserted into the grid.
`property OnColRowMoved: TGridOperationEvent;` |  | Event handler signalled when a column or row in the grid is moved.
`property OnCompareCells: TOnCompareCells;` |  | Event handler signalled to compare the content in cells
`property OnContextPopup: TContextPopupEvent;` |  | Invoked when a context-sensitive pop-up menu is requested.
`property OnDragDrop: TDragDropEvent;` |  | Event handler signalled when an object is dropped onto the control.
`property OnDragOver: TDragOverEvent;` |  | Event handler signalled when a control is dragged over the control instance.
`property OnDblClick: TNotifyEvent;` |  | Event handler signalled when a mouse double click occurs in the control.
`property OnDrawCell: TOnDrawCell;` |  | Event handler signalled to draw a cell in the grid control.
`property OnEditButtonClick: TNotifyEvent; deprecated ;` |  | Deprecated. Use OnButtonClick instead.
`property OnButtonClick: TOnSelectEvent;` |  | Event handler signalled when a button-style cell Editor control is clicked.
`property OnEditingDone: TNotifyEvent;` |  | Event handler signalled when editing is completed for the control.
`property OnEndDock: TEndDragEvent;` |  | Event handler signalled for the end of a drag-dock operation.
`property OnEndDrag: TEndDragEvent;` |  | Event handler signalled for the end of a drag-drop operation.
`property OnEnter: TNotifyEvent;` |  | Event handler signalled when the control receives focus.
`property OnExit: TNotifyEvent;` |  | Event handler signalled when the control loses focus.
`property OnGetCellHint: TGetCellHintEvent;` |  | Gets the hint text for a cell.
`property OnGetCheckboxState: TGetCheckboxStateEvent;` |  | Event handler signalled to get the state for a check box cell.
`property OnGetEditMask: TGetEditEvent;` |  | Event handler signalled to get the edit mask used for a grid cell.
`property OnGetEditText: TGetEditEvent;` |  | Event handler signalled to get the value for a cell editor in the grid.
`property OnHeaderClick: THdrEvent;` |  | Event handler signalled when the fixed header for a column or row is clicked.
`property OnHeaderSized: THdrEvent;` |  | Event handler signalled when a column or row header has been resized.
`property OnHeaderSizing: THeaderSizingEvent;` |  | Event handler signalled when a column or row header sizing action is started.
`property OnKeyDown: TKeyEvent;` |  | Event handler signalled for key down keyboard events.
`property OnKeyPress: TKeyPressEvent;` |  | Event handler signalled for character data entered by the user.
`property OnKeyUp: TKeyEvent;` |  | Event handler signalled when a key up event has occurred for the control.
`property OnMouseDown: TMouseEvent;` |  | Event handler signalled when a mouse down event is handled for the control.
`property OnMouseEnter: TNotifyEvent;` |  | Event handler signalled when the mouse pointer has entered the control.
`property OnMouseLeave: TNotifyEvent;` |  | Event handler signalled when the mouse pointer has left the control.
`property OnMouseMove: TMouseMoveEvent;` |  | Event handler signalled when the mouse pointer is moved in the control.
`property OnMouseUp: TMouseEvent;` |  | Event handler signalled when a mouse up event is handled for the control.
`property OnMouseWheel: TMouseWheelEvent;` |  | Event handler for mouse wheel turned.
`property OnMouseWheelDown: TMouseWheelUpDownEvent;` |  | Event handler signalled for a downward movement of the mouse wheel.
`property OnMouseWheelUp: TMouseWheelUpDownEvent;` |  | Event handler signalled for an upward movement of the mouse wheel.
`property OnMouseWheelHorz: TMouseWheelEvent;` |  | Event handler signalled for a horizontal movement of the mouse wheel.
`property OnMouseWheelLeft: TMouseWheelUpDownEvent;` |  | Event handler signalled for a leftward movement of the mouse wheel.
`property OnMouseWheelRight: TMouseWheelUpDownEvent;` |  | Event handler signalled for a rightward movement of the mouse wheel.
`property OnPickListSelect: TNotifyEvent;` |  | Event handler signalled when an item is selected from a pick list-style Editor control.
`property OnPrepareCanvas: TOnPrepareCanvasEvent;` |  | Event handler signalled to prepare the Canvas to draw the grid.
`property OnResize: TNotifyEvent;` |  | Notification handler for a resize of the control.
`property OnSelectEditor: TSelectEditorEvent;` |  | Event handler signalled to select an Editor control for the current grid cell.
`property OnSelection: TOnSelectEvent;` |  | Event handler signalled when an area is selected in the grid control.
`property OnSelectCell: TOnSelectCellEvent;` |  | Event handler signalled when a grid cell is selected.
`property OnSetCheckboxState: TSetCheckboxStateEvent;` |  | Event handler signalled to set the state for a check box cell.
`property OnSetEditText: TSetEditEvent;` |  | Event handler signalled when the text in the cell Editor is assigned.
`property OnShowHint: TControlShowHintEvent;` |  | Event handler signalled when a hint window is displayed for the control.
`property OnStartDock: TStartDockEvent;` |  | Event handler for the start of a docking operation.
`property OnStartDrag: TStartDragEvent;` |  | Event handler signalled for the start of a dragging operation.
`property OnTopLeftChanged: TNotifyEvent;` |  | Event handler signalled when the top left cell in the visible area of the control is changed.
`property OnUserCheckboxBitmap: TUserCheckBoxBitmapEvent;` |  | Event handler signalled to get the user-defined bitmap used for a check box cell.
`property OnUserCheckboxImage: TUserCheckBoxImageEvent;` |  | Event handler signalled to get the user-defined image used for a check box cell.
`property OnUTF8KeyPress: TUTF8KeyPressEvent;` |  | Handler for a character entered by the user.
`property OnValidateEntry: TValidateEntryEvent;` |  | Event handler signalled to perform validation for a cell value.
`end;`

## Inheritance

`TStringGrid` |  | Implements a grid specialized for textual content.
`TCustomStringGrid` |  | The base class for TStringGrid.
`TCustomDrawGrid` |  | The base class for a custom-drawn grid control.
`TCustomGrid` |  | Implements the base class for grid controls.
`TCustomControl` |  | The base class for windowed controls which paint themselves.
`TWinControl` |  | Implements a windowed control which can contain other child controls.
`TControl` |  | The base class for visible controls.
`TLCLComponent` |  | The base class for LCL components which have an associated widget.
`TComponent`
?
`TObject`

## Description

TStringGrid is a TCustomStringGrid descendant which implements a specialized grid for displaying textual content in a matrix of columns and rows.

TStringGrid is designed for use with string content stored in its cells. Internally, columns and rows in the class are mapped to TStringGridStrings instances used to store the values in the grid control. TStringGrid also allows a TObject instance to be stored for each cell in the grid using the Objects property.

TStringGrid sets the visibility for properties defined in the ancestor class. It has an overridden WSRegisterClass method to register properties (from older LCL versions) which are no longer used in LCL component streaming.

Key properties:

The ColCount and RowCount properties contain the column and row count of the grid.

The FixedCols and FixedRows properties specify the count of fixed columns or rows that are used for headings.

The column widths and row heights of the grid are accessible with the ColWidths and RowHeights properties.

The DefaultColWidth and DefaultRowHeight properties are used to specify default column widths or row heights respectively.

The colors used for the cells and other grid elements are specified in the following properties:

- AlternateColor
- BorderColor
- FixedColor
- FixedHotColor
- FocusColor
- GridLineColor

The GridWidth and GridHeight properties contain the dimension of the entire grid.

The ScrollBars controls the creation of scroll bars for the grid.

The LeftCol, TopRow, VisibleColCount and VisibleRowCount properties contain information about the visible area of the grid.

The Options controls options for the grid.

Key methods and events:

If the user highlights a cell of the grid, the SelectCell method is called that triggers the OnSelectCell event. The position of the highlighted cell is stored within the Col and Row property.

The MouseToCell method calculates a grid cell from a given screen position.

Huge changes to the grid should be encapsulated in calls to BeginUpdate and EndUpdate to speed up the application.

Component developers must override the DrawCell method in customized grid controls.

Grid cell selection

The location of the current (focused) cell (or row) can be changed using keyboard, mouse or through code. In order to change cell focus successfully to another position, we must test the target position to see if it is allowed to receive cell focus. When using keyboard, the property AutoAdvance performs part of the process by finding what should be the next focused cell. When using mouse clicks or moving by code, focus will not move from the current cell unless the target cell is permitted to receive focus.

The grid calls the SelectCell function to see if a cell is focusable: if this function returns true, then the target cell identified with arguments aCol and aRow is focusable (the current implementation of TCustomGrid simply returns true). TCustomDrawGrid and hence TDrawGrid and TStringGrid override this method to check first if cell is any wider than 0; normally you don't want a 0 width cell selected so a cell with these characteristics is skipped automatically in the process of finding a suitable cell. The other thing the overridden method SelectCell does is to call the user configurable event OnSelectCell: this event receives the cell coordinates as arguments and always returns a default result value of true.

Once a cell is known to be focusable and we are sure a movement will take place, first the method BeforeMoveSelection is called; this in turns triggers the OnBeforeSelection event. This method's arguments are the coordinates for the new focused cell. At this point any visible editor is hidden too. The "before" word means that selection is not yet changed and current focused coordinates can be accessed with grid.Col and grid.Row properties.

After that, the internal focused cell coordinates are changed and then MoveSelection method is called; this method's purpose is to trigger the OnSelection event if set (this is a notification that the focused cell has, by this time, already changed and cell coordinates are now available through grid.row and grid.col properties).

Note that is not good to use OnSelectCell event to detect cell focus changes, as this event will be triggered several times even for the same cell in the process of finding a suitable cell. Is better to use OnBeforeSelection or OnSelection events for this purpose.

Multi-selection

The Lazarus grids are able to extend the selection to multiple cells. The Option goRangeSelect must be active for this purpose: Now you can drag the mouse over a rectangle on the grid to select all the cells included or intersected by this rectangle. Or you can click on the cell in one corner, hold SHIFT down and either click on the opposite corner of the selection, or press the arrow keys to extend the selection in the corresponding direction.

In the default setting of the RangeSelectMode property, rsmSingle, only a single cell or cell range (rectangle of cells) can be selected, as described. The rsmMulti mode, however, allows selection of multiple and even discontiguous cells or ranges. After selecting the first cell/range as already described, hold the CTRL key down and click on the next cell or drag over the next range to be selected. Instead of dragging you can also select an additional range by pressing SHIFT (in addition to CTRL) and clicking on the opposite corner of the range or pressing the arrow keys.

The selected cells are highlighted by a different color. Note, however, that the currently focused cell has the default background color of the grid unless you set the Option goDrawFocusSelected.

Clicking somewhere on an unselected cell unselects the selected cell(s)/range( s).

If you need to access selections by code you can do this by using these TCustomGrid properties:

SelectedRange[AIndex: Integer]: TGridRect:
  Returns the column/row coordinates of the corner cells of the selected range at the specified index (type TGridRect = TRect). A single selected cell is considered to be a block with equal Left and Right column and equal Top and Bottom row coordinates.
SelectedRangeCount: Integer:
  The number of selected blocks.

Note that these are read-only properties, you can add and change selections only by user interaction. But you can call ClearSelections to unselect everything.

Your selection handling code must be prepared that a selected cell could be found in another selected block again. Normally this situation is prohibited in the user interface, but there are scenarios where a selection could be extended into another range so that the overlapping cells are "selected twice".

Additional information about grid usage is available on the Lazarus wiki in the following pages:

- Grids Reference Page
- TDrawGrid
- TStringGrid

## See also

TCustomStringGrid |  | The base class for TStringGrid.
HowToUseGrids |  | How to use Grids including StringGrids, DrawGrids and DbGrids.