Recent

Author Topic: Copy a cell from a TDBGrid  (Read 1213 times)

lcapitanache

  • Newbie
  • Posts: 5
Copy a cell from a TDBGrid
« on: November 21, 2024, 04:59:53 pm »
Hi,

I have an application with a TDBGrid and I want to implement a functionality that allows to copy the content of a cell:

1. The user right clicks on a cell.
2. A TPopMenu appears with several options, including “Copy cell”.
3. When selecting “Copy cell”, the content is copied to the clipboard.

The desired behavior is very simple, but I am having problems with the implementation, especially getting the correct cell value.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   Coord: TGridCoord;
  5.   CellValue: String;
  6.   Tmp: Integer;
  7. begin
  8.   if Button = mbRight then
  9.   begin
  10.     Coord:= DBGrid1.MouseCoord(X, Y);
  11.  
  12.     if (Coord.X >= 0) and (Coord.Y >= 0) then
  13.     begin
  14.       Tmp:= DBGrid1.DataSource.DataSet.RecNo;
  15.  
  16.       DBGrid1.DataSource.DataSet.RecNo:= Coord.Y;
  17.       CellValue:= DBGrid1.Columns[Coord.X].Field.AsString;
  18.  
  19.       DBGrid1.DataSource.DataSet.RecNo:= Tmp;
  20.  
  21.       ShowMessage(CellValue);
  22.     end;
  23.   end;
  24. end;

With this approach I get the correct value of any cell, but I have the following problems:

1. If the user previously left-clicks anywhere in the lower second half of the TDBGrid, and then right-clicks on the cell he wants to copy, the TDBGrid scrolls and returns an incorrect cell value.

2. If the cell you want to copy is further down in the TDBGrid and you therefore need to scroll down, the cell value returned is incorrect.

I understand that (at least part of) the problem lies in the RecNo:= Coord.Y assignment, since Coord.Y returns the location of the cursor on the Y axis, and this only matches the first RecNo values when the TDBGrid is on the first screen. However, I confess that I can't think of any other way to get the desired cell location.

Could someone please help me with this?

Sieben

  • Sr. Member
  • ****
  • Posts: 372
Re: Copy a cell from a TDBGrid
« Reply #1 on: November 21, 2024, 05:56:38 pm »
You will have to make the cell in question the selected cell of the grid by setting it's Row and Col properties according to your Coords in order to get proper access to the underlying record. You might then as well use the grids DoCopyToClipboard method.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

lcapitanache

  • Newbie
  • Posts: 5
Re: Copy a cell from a TDBGrid
« Reply #2 on: November 21, 2024, 10:21:05 pm »
You will have to make the cell in question the selected cell of the grid by setting it's Row and Col properties according to your Coords in order to get proper access to the underlying record. You might then as well use the grids DoCopyToClipboard method.

I have been trying but I am unable to set the selection by code. As I understand it, the TDBGrid does not have methods to directly access the Col and Row properties. Could you provide an example, please?

wp

  • Hero Member
  • *****
  • Posts: 12530
Re: Copy a cell from a TDBGrid
« Reply #3 on: November 21, 2024, 10:45:31 pm »
The field behind the highlighted cell in the grid can be accessed as DBGrid.SelectedField. Therefore, copying a cell string to the clipboard boils down to
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem1Click(Sender: TObject);
  2. var
  3.   field: TField;
  4. begin
  5.   field := DBGrid1.SelectedField;
  6.   if Assigned(field) then
  7.     Clipboard.AsText := field.AsString;
  8. end;
However, you must make sure  that the right-click to open the popup menu moves the selection to the clicked cell. This happens without any further code when you add the option dgAnyButtonCanSelect to the grid's Options.

Sieben

  • Sr. Member
  • ****
  • Posts: 372
Re: Copy a cell from a TDBGrid
« Reply #4 on: November 21, 2024, 10:52:54 pm »
Unfortunately a right click on a cell does not by itself highlight / select it.
« Last Edit: November 21, 2024, 10:54:33 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

wp

  • Hero Member
  • *****
  • Posts: 12530
Re: Copy a cell from a TDBGrid
« Reply #5 on: November 21, 2024, 11:17:49 pm »
Unfortunately a right click on a cell does not by itself highlight / select it.
Did you read the last part of my post?

Sieben

  • Sr. Member
  • ****
  • Posts: 372
Re: Copy a cell from a TDBGrid
« Reply #6 on: November 22, 2024, 12:00:04 am »
Oh, sorry, I obviously did not.
« Last Edit: November 22, 2024, 12:03:31 am by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

lcapitanache

  • Newbie
  • Posts: 5
Re: Copy a cell from a TDBGrid
« Reply #7 on: November 22, 2024, 03:52:48 pm »
The field behind the highlighted cell in the grid can be accessed as DBGrid.SelectedField. Therefore, copying a cell string to the clipboard boils down to
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem1Click(Sender: TObject);
  2. var
  3.   field: TField;
  4. begin
  5.   field := DBGrid1.SelectedField;
  6.   if Assigned(field) then
  7.     Clipboard.AsText := field.AsString;
  8. end;
However, you must make sure  that the right-click to open the popup menu moves the selection to the clicked cell. This happens without any further code when you add the option dgAnyButtonCanSelect to the grid's Options.

It’s working flawlessly! Thank you so much!

 

TinyPortal © 2005-2018