Table of Contents

Clipboard

The grid round-trips with Excel out of the box: copy a selection as tab-delimited text, paste tab-delimited text from anywhere. Control whether pasted data replaces or inserts rows, whether the grid can grow to fit, and whether read-only cells are skipped.

Overview

Three operations, all keyboard-shortcut friendly:

Operation Method Keyboard
Copy Grid.CopyToClipboard Ctrl+C
Cut Grid.CutToClipboard Ctrl+X
Paste Grid.PasteFromClipboard Ctrl+V

The clipboard format is tab-separated values with newline-separated rows — the same format Excel writes — so paste-into-Excel and paste-from-Excel both just work.

Quick example

procedure TForm1.FormCreate(Sender: TObject);
begin
  Grid.Options.Clipboard.IncludeHeaders := True;     // include the header row
  Grid.Options.Clipboard.OnlySelected   := True;     // only copy the selection
end;

procedure TForm1.CopyButtonClick(Sender: TObject);
begin
  Grid.CopyToClipboard;
end;

procedure TForm1.PasteButtonClick(Sender: TObject);
begin
  Grid.PasteFromClipboard;
end;

Configuration

Grid.Options.Clipboard exposes the paste-behaviour toggles:

procedure TForm1.ConfigureClipboardPaste;
begin
  Grid.Options.Clipboard.AllowRowGrow := True;
  Grid.Options.Clipboard.AllowColumnGrow := True;
  Grid.Options.Clipboard.PasteAction := gpaInsert;
  Grid.Options.Clipboard.IgnoreReadOnly := False;
end;
Option Default Effect
AllowRowGrow False When the pasted block has more rows than fit, new rows are appended automatically.
AllowColumnGrow False When the pasted block has more columns than fit, new columns are appended automatically.
IgnoreReadOnly False When True, read-only cells are overwritten during paste.
PasteAction gpaReplace gpaReplace overwrites existing cell values; gpaInsert inserts new rows before writing the pasted data.

Allow the grid to grow on paste

Useful when pasting blocks larger than the current grid dimensions — the grid expands rather than truncating the pasted data.

Insert rows instead of replacing

With gpaInsert, the grid inserts new rows at the paste target before writing the pasted data, preserving the rows that were already there.

Paste into read-only cells

By default, cells with ReadOnly = True are skipped during paste. Set IgnoreReadOnly to force overwrite even on those cells.

Hooks

Event Use it for
OnBeforeCopyToClipboard, OnBeforeCutToClipboard, OnBeforePasteFromClipboard Validate or cancel the operation.
OnAfterCopyToClipboard, OnAfterCutToClipboard, OnAfterPasteFromClipboard Post-processing (e.g. re-apply formatting after paste).
procedure TForm1.GridBeforePaste(Sender: TObject; var ACanPaste: Boolean);
begin
  if Grid.SelectedCellCount = 0 then
    ACanPaste := False;
end;

Custom keyboard shortcuts

Ctrl+C/X/V are wired automatically when Options.Clipboard.Enabled is True (the default). To call paste from your own UI button, call Grid.PasteFromClipboard directly — it does not depend on the keyboard mapping.

Combining clipboard configuration, hooks, and a toolbar button

This example configures clipboard behavior, intercepts paste to validate data, and wires a toolbar button as an alternative paste trigger — covering all three clipboard features in one setup:

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Configuration: allow paste to add new rows, replace existing content
  Grid.Options.Clipboard.AllowRowGrow  := True;
  Grid.Options.Clipboard.PasteAction  := gpaReplace;
  Grid.Options.Clipboard.IgnoreReadOnly := False;

  // Hook: validate pasted data before committing
  Grid.OnBeforePasteFromClipboard := GridBeforePasteFromClipboard;

  // Toolbar button as alternative paste trigger (no keyboard required)
  PasteButton.OnClick := PasteButtonClick;
end;

procedure TForm1.GridBeforePasteFromClipboard(Sender: TObject;
  var AAllow: Boolean);
begin
  // Reject paste when the grid is in a read-only view mode
  AAllow := not ReadOnlyMode;
end;

procedure TForm1.PasteButtonClick(Sender: TObject);
begin
  Grid.PasteFromClipboard;
end;
  • Grid.CopyToClipboard
  • Grid.CutToClipboard
  • Grid.PasteFromClipboard
  • Grid.Options.Clipboard.AllowRowGrow / AllowColumnGrow
  • Grid.Options.Clipboard.IgnoreReadOnly
  • Grid.Options.Clipboard.PasteAction (gpaReplace / gpaInsert)
  • OnBeforeCopyToClipboard, OnAfterCopyToClipboard
  • OnBeforeCutToClipboard, OnAfterCutToClipboard
  • OnBeforePasteFromClipboard, OnAfterPasteFromClipboard

See also