Recent

Author Topic: Showing hint with content of a cell in a DBGrid (SOLVED)  (Read 5630 times)

derles

  • New Member
  • *
  • Posts: 35
Showing hint with content of a cell in a DBGrid (SOLVED)
« on: June 27, 2017, 05:48:44 pm »
Hello all.
I have the need of showing a hint with the content of a cell of a DBGrid when the mouse moves over the cell (not current cell).
I was using Lazarus 1.0.10 and FPC 2.6.2, and solved the issue with this code, simulating the hint with a memo control:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyGrid = Class(TCustomDBGrid);
  3.  
  4. procedure TForm1.dbgrid1MouseMove(Sender: TObject;Shift: TShiftState; X, Y: Integer);
  5. var
  6.   row, col: integer;
  7.   rec: integer;
  8. begin
  9.   inherited;
  10.  
  11.   TDBGrid(Sender).MouseToCell(X, Y, col, row);
  12.  
  13.   if dgTitles in TDBGrid(Sender).Options then
  14.     Dec(row);
  15.  
  16.   if TMyGrid(TDBGrid(Sender)).DataLink.Active and (row >= 0) and (col = 2) then
  17.   begin
  18.     rec := TMyGrid(TDBGrid(Sender)).DataLink.ActiveRecord;
  19.  
  20.     try
  21.       TMyGrid(TDBGrid(Sender)).DataLink.ActiveRecord := row;
  22.  
  23.       Memo1.Text := TDBGrid(Sender).Columns[col].Field.AsString;
  24.       Memo1.Visible := True;
  25.     finally
  26.       TMyGrid(TDBGrid(Sender)).DataLink.ActiveRecord := rec;
  27.     end;
  28.   end
  29.   else
  30.   begin
  31.     Memo1.Text := '';
  32.     Memo1.Visible := False;
  33.   end;
  34. end;
  35.  

Last week I updated Lazarus to 1.6.4, FPC 3.02, and in the same code I have a typecast error at compliling:
Error: Class or Object types "TDBGrid" and "TMyGrid" are not related
in every line with the code TMyGrid(TDBGrid(Sender)).DataLink.Active

If I replace
TMyGrid(TDBGrid(Sender)).DataLink.Active
with
TMyGrid(TCustomDBGrid(Sender)).DataLink.Active
i have no error at compliling, but this error at runtime:
Invalid TypeCast

If not use TMyGrid, using just the typecast
TCustomDBGrid(Sender).DataLink.Active
i have this compliling error:
Error: identifier idents no member "DataLink"


After a few days working on it, I still can't figure out how to solve this.
Any suggestion ?

I use Firebird and ZeosLib.

TKS !
« Last Edit: June 27, 2017, 10:10:33 pm by derles »

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Showing hint with content of a cell in a DBGrid
« Reply #1 on: June 27, 2017, 06:03:25 pm »
Forget this old code, cell hints have been built in some time ago: http://wiki.lazarus.freepascal.org/Grids_Reference_Page#Cell_hints

Besides that, the "hacking" type-cast is wrong. In order to access a protected property of a component you must declare a descendant of this component, not of its ancestor, i.e. you must declare TMyGrid = class(TDBGriid) not class(TDBCustomGrid). If this had been working in Laz 1.0.10 then it was a bug.

Instead of the double cast do a single cast. TMyGrid(Sender).DataLink.ActiveRecord.

You could also work with bookmarks.
« Last Edit: June 27, 2017, 06:24:48 pm by wp »

derles

  • New Member
  • *
  • Posts: 35
Re: Showing hint with content of a cell in a DBGrid (SOLVED)
« Reply #2 on: June 27, 2017, 10:09:33 pm »
Now, this was a truly deeply helpful reply. My grid now works as I dreamed.

Thank you very much wp.

 

patyi

  • Full Member
  • ***
  • Posts: 168
Re: Showing hint with content of a cell in a DBGrid (SOLVED)
« Reply #3 on: November 01, 2017, 05:04:08 pm »
Hi !

I have error: invalid type cast in this code.
i386 XUbuntu 17.10, Lazarus 1.8.0RC4 from 09.10.2017, FPC 3.03 installed with FpcUpDelux Fixex branch.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyGrid = class(TDBGrid);
  3.  
  4. procedure TForm.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  5.   Y: Integer);
  6. var
  7.   row, col : integer;
  8.   rec : integer;
  9. begin
  10.   inherited;
  11.  
  12.   TDBGrid(Sender).MouseToCell(X, Y, col, row);
  13.   if dgTitles in TDBGrid(Sender).Options then
  14.     Dec(row);
  15.   if dgIndicator in TDBGrid(Sender).Options then
  16.     Dec(col);
  17.  
  18.   if TMyGrid(Sender).DataLink.Active and (row >= 0) and (col >= 0) then begin  // here is error : Invalid type cast
  19.     rec := TMyGrid(Sender).DataLink.ActiveRecord;
  20.     try
  21.       TMyGrid(Sender).DataLink.ActiveRecord := row;
  22.     finally
  23.       TMyGrid(Sender).DataLink.ActiveRecord:= rec;
  24.     end;
  25.   end;
  26. end;  

I no idea wat is wrong !  Thanks.
« Last Edit: November 01, 2017, 05:17:00 pm by patyi »

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Showing hint with content of a cell in a DBGrid (SOLVED)
« Reply #4 on: November 01, 2017, 05:57:18 pm »
It is working for me. But I guess that the event is not generated by a DBGrid. Instead of a type-cast (TDBGrid(Sender)) use operator "as" (Sender as TDBGrid) - it raises an exception if the sender is not a DBGrid. To further help you with debugging you can also check the class by "is" and show a message with the true class name

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState;
  2.   X, Y: Integer);
  3. var
  4.   row, col : integer;
  5.   rec : integer;
  6. begin
  7.   inherited;
  8.  
  9.   if not (Sender is TDBGrid) then
  10.     ShowMessage('Sender is not a TDBGrid but a ' + sender.ClassName + '. Its name is ' + Sender.Name);
  11.  
  12.   ...
« Last Edit: November 02, 2017, 12:19:55 am by wp »

patyi

  • Full Member
  • ***
  • Posts: 168
Re: Showing hint with content of a cell in a DBGrid (SOLVED)
« Reply #5 on: November 01, 2017, 11:20:07 pm »
Event is generated by DBGrid !  Traing to insert suggested message about class name, no message is paper that Sender is not TDBGrid.

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Showing hint with content of a cell in a DBGrid (SOLVED)
« Reply #6 on: November 02, 2017, 12:26:29 am »
Then you must try to extract the DBGrid part of your project into a small demo which shows the error. Pack .pas, .lfm, lpi, lpr and database file into a shared zip which can be uploaded here (no compiler-generated files, please). And it would be fine if you could show the issue with a little standard database (dbf, memorydataset, bufdataset, sqlite3, or so), no big server system.

patyi

  • Full Member
  • ***
  • Posts: 168
Re: Showing hint with content of a cell in a DBGrid (SOLVED)
« Reply #7 on: November 02, 2017, 10:59:33 pm »
Hi !
Now it is working ! If you are using DEBUG mode, the -CR compiler option must be switched off !
Everything is OK if you use DEFAULT or RELEASE modes, because the -CR is off by default ...

Thank you wp for pointing me to the problem!

 

TinyPortal © 2005-2018