Forum > Databases
Copy a cell from a TDBGrid
lcapitanache:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfrmMain.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);var Coord: TGridCoord; CellValue: String; Tmp: Integer;begin if Button = mbRight then begin Coord:= DBGrid1.MouseCoord(X, Y); if (Coord.X >= 0) and (Coord.Y >= 0) then begin Tmp:= DBGrid1.DataSource.DataSet.RecNo; DBGrid1.DataSource.DataSet.RecNo:= Coord.Y; CellValue:= DBGrid1.Columns[Coord.X].Field.AsString; DBGrid1.DataSource.DataSet.RecNo:= Tmp; ShowMessage(CellValue); end; end;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:
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.
lcapitanache:
--- Quote from: Sieben 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.
--- End quote ---
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.MenuItem1Click(Sender: TObject);var field: TField;begin field := DBGrid1.SelectedField; if Assigned(field) then Clipboard.AsText := field.AsString;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:
Unfortunately a right click on a cell does not by itself highlight / select it.
Navigation
[0] Message Index
[#] Next page