Question

I'm working on a tool which connects to a SQL Database, gets back a dataset, and displays that data in a grid. The user must be able to select a block of cells (just rectangular) and press CTRL+C to copy it to the clipboard.

How do I do this:

  • In a format that can be pasted into Excel? I'm hoping there's already something ready-made for this. It doesn't need all the clipboard features like Excel, just highlighting a rectangular group of cells and copying it to the clipboard.

  • If it can be done in a TStringGrid I would prefer to keep my functionality in that, but could also work with a component which supports this.

Was it helpful?

Solution

You can try to copy you cell values as TAB delimited text, something like this code does:

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
  X, Y: Integer;
begin
  S := '';
  for Y := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
  begin
    for X := StringGrid1.Selection.Left to StringGrid1.Selection.Right - 1 do
      S := S + StringGrid1.Cells[X, Y] + #9;
    S := S + StringGrid1.Cells[StringGrid1.Selection.Right, Y] + sLineBreak;
  end;
  Delete(S, Length(S) - Length(sLineBreak) + 1, Length(sLineBreak));
  Clipboard.AsText := S;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top