Recent

Author Topic: Column for DBGrid double-click?  (Read 5099 times)

KarenT

  • Full Member
  • ***
  • Posts: 120
Column for DBGrid double-click?
« on: November 13, 2018, 08:50:23 pm »
Hello, I need to change (toggle) a checkbox in an SQLite DB that is displayed in a TDBGrid. I am using the CellClick for changing the Filtering so I have tried to use the DblClick for changing the checkbox. But, that function brings nothing with it like a Column.

How can I tell which Column was just DblClicked so that I can toggle the SQL data?

Thanks

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Column for DBGrid double-click?
« Reply #1 on: November 13, 2018, 09:19:51 pm »
This is what i use for the Row.
Code: Pascal  [Select][+][-]
  1.   wID := StrToInt (Grid_wedstrijden.Cells[0, Grid_wedstrijden.Row]);
  2.  
This is for a normal grid
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

wp

  • Hero Member
  • *****
  • Posts: 11856
Re: Column for DBGrid double-click?
« Reply #2 on: November 13, 2018, 10:42:27 pm »
How can I tell which Column was just DblClicked so that I can toggle the SQL data?
Get the screen coordinates of the mouse position from the global Mouse instance (Mouse.CursorPos), convert it to grid-internal coordinates (DBGrid1.ScreenToClient(Mouse.CursorPos)) and stuff that into the DBGrid method MouseToRecordOffset which returns the TColumn of the click.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1DblClick(Sender: TObject);
  2. var
  3.   col: TColumn;
  4.   recoffs: Integer;
  5.   P : TPoint;
  6. begin
  7.   P := DBGrid1.ScreenToClient(Mouse.CursorPos);
  8.   DBGrid1.MouseToRecordOffset(P.X, P.Y, col, recoffs);
  9.   if col = nil then
  10.     ShowMessage('no col')
  11.   else
  12.     ShowMessage(col.FieldName + ': ' + col.Field.AsString);
  13. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Column for DBGrid double-click?
« Reply #3 on: November 13, 2018, 10:54:06 pm »
How can I tell which Column was just DblClicked so that I can toggle the SQL data?

Doesn't the Grid.Col property give you that?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11856
Re: Column for DBGrid double-click?
« Reply #4 on: November 13, 2018, 11:43:58 pm »
How can I tell which Column was just DblClicked so that I can toggle the SQL data?

Doesn't the Grid.Col property give you that?

Hmmm... I don't know for sure. Grid.Col is the column index, not a pointer to a TColumn. What if columns are hidden or re-arranged? Is Grid.Col then the physical column index of the selected cell or the index of the TColumn in the original order? I'd guess it is the former one, but to avoid experimentation I thought it is safer to use the direct method of TDBGrid which directly returns a TColumn.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Column for DBGrid double-click?
« Reply #5 on: November 14, 2018, 12:11:48 am »
Is Grid.Col then the physical column index of the selected cell or the index of the TColumn in the original order? I'd guess it is the former one, but to avoid experimentation I thought it is safer to use the direct method of TDBGrid which directly returns a TColumn.

Yes, sorry, I hadn't seen your reply. I don't know for certain either--which why I simply asked--but according to the wiki (talking of OnEditButtonClick):

Quote
[...] find out in which cell a button has been clicked by taking a look at the grid.Row and grid.Col properties

And in all the examples of handlers which get passed a column and a row, they get compared to Grid.Col and Grid.Row to see if it's the selected one so ... one may guess that it is the physical one? Does anyone know for sure?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Column for DBGrid double-click?
« Reply #6 on: November 14, 2018, 12:17:12 pm »
Just test it and you will know :D
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

KarenT

  • Full Member
  • ***
  • Posts: 120
Re: Column for DBGrid double-click?
« Reply #7 on: November 14, 2018, 05:53:27 pm »
Get the screen coordinates of the mouse position from the global Mouse instance (Mouse.CursorPos), convert it to grid-internal coordinates

True to your rating you are **my** hero! :)

That code actually also solved a gripe I have had with the Grids for years, a click in the blank area below the drawn grid, if there is any blank there.

"no col" --- AWESOME, thank you.


KarenT

  • Full Member
  • ***
  • Posts: 120
Re: Column for DBGrid double-click?
« Reply #8 on: November 14, 2018, 05:55:21 pm »
This is for a normal grid

Thanks, but code in the post by **wp** solved my issue and nicely covered another problem as well.

 

TinyPortal © 2005-2018