Recent

Author Topic: <SOLVED>How to copy select Row data from DBGrid?  (Read 1095 times)

Jonvy

  • Jr. Member
  • **
  • Posts: 90
<SOLVED>How to copy select Row data from DBGrid?
« on: June 21, 2022, 04:10:17 pm »
Dear all,
From the DBGrid, I get the data from SQL query, but I want to copy the data for later use.
From pgAdmin tools, I can select the rows,then just press 'Ctrl+C' from keyboard, then paste to memo or note pad.
I want to do same for my program, how to do this?Should set some options for DBGrid or I need programming?

Can anyone give me some ideas?
Thanks!

Best regards,
Jonvy
« Last Edit: June 27, 2022, 04:33:27 am by Jonvy »

dsiders

  • Hero Member
  • *****
  • Posts: 1080
Re: How to copy select Row data from DBGrid?
« Reply #1 on: June 21, 2022, 06:59:57 pm »
Dear all,
From the DBGrid, I get the data from SQL query, but I want to copy the data for later use.
From pgAdmin tools, I can select the rows,then just press 'Ctrl+C' from keyboard, then paste to memo or note pad.
I want to do same for my program, how to do this?Should set some options for DBGrid or I need programming?

Can anyone give me some ideas?
Thanks!

Best regards,
Jonvy

Most of the grid controls already support Ctrl+C to copy to clipboard. However, TDBGrid acts on a single cell and not the entire row. You could implement an OnKeyDown handler in the grid to do what you need... which is basically to re-implement the DoCopyToClipboard method to behave like the one in TCustomStringGrid where multiple cells are allowed.

Look at TCustomStringGrid.CopyCellRectToClipboard for ideas.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: How to copy select Row data from DBGrid?
« Reply #2 on: June 22, 2022, 09:06:33 am »
You dbgrid is connected to a datasource -> dataset? Then you know the selected row and can easily copy a row to append the selected row.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to copy select Row data from DBGrid?
« Reply #3 on: June 22, 2022, 09:52:55 am »
Since DBGrid inherits a virtual method DoCopyToClipboard from its ultimate ancestor, TCustomGrid, you can solve this by subclassing:
Code: Pascal  [Select][+][-]
  1. type
  2.   TDBGrid = class(DBGrids.TDBGrid)
  3.   protected
  4.     procedure DoCopyToClipboard; override;
  5.   end;
  6.  
  7. procedure TDBGrid.DoCopyToClipboard;
  8. var
  9.   s: String;
  10.   c: Integer;
  11. begin
  12.   s := '';
  13.   for c := 0 to Columns.Count-1 do
  14.     s := s + #9 + Columns[c].field.AsString;
  15.   if s <> '' then Delete(s, 1, 1);
  16.   Clipboard.AsText := s;
  17. end;

Put the redeclaration of TDBGrid into the same unit in which you want to apply it, and put it in front of the first usage of a DBGrid instance. Or, if there are several places which you want to be handled in the same way, put it into a separate unit and mention this unit at the end of the uses list of all forms needing it. This way the compiler is "deceived", and he executes the modified DoCopyToClipbpoard rathter than the original one when CTRL+C is pressed.

Jonvy

  • Jr. Member
  • **
  • Posts: 90
Re: How to copy select Row data from DBGrid?
« Reply #4 on: June 23, 2022, 03:33:59 pm »
Thanks wp! Your code solve my problem.

 

TinyPortal © 2005-2018