Recent

Author Topic: [Solved] Colors in a DBGrid [Yes Really]  (Read 22027 times)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Colors in a DBGrid
« Reply #15 on: December 20, 2017, 01:33:55 pm »
Yes but my brains are not working properly to get a solution  %)


My brains knows the solution but i can't program it
it something like this
if mod 3 = 0 then change colour overtime
over time? really? so what happens if you color the first row again after the grid was open for a half an hour? the lines change colors? Think it over.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #16 on: December 20, 2017, 01:48:41 pm »
it should be every time
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Colors in a DBGrid
« Reply #17 on: December 20, 2017, 01:53:51 pm »
it should be every time
care to correct your requirements and restate your use case for colors?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 13515
Re: Colors in a DBGrid
« Reply #18 on: December 20, 2017, 01:55:37 pm »
Suppose you have a series of number 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... Which math operation is needed to group these numbers by 3?
Which operation do you have to perform on these numbers to construct a series like 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, ... Then you need another operation to convert this series to 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, .... Which one?

Hint: mod 3 is in the right direction, but not exactly. -- End of hints.

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #19 on: December 20, 2017, 02:58:39 pm »
That should be the integer divide (div)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

wp

  • Hero Member
  • *****
  • Posts: 13515
Re: Colors in a DBGrid
« Reply #20 on: December 20, 2017, 03:04:35 pm »
Yes, the operation div 3 converts the series 0,1,2,3,4,5,6,7,8,9,10,11,12,... to blocks of 3 numbers: 0,0,0,1,1,1,2,2,3,4,4,4,...
 
But you have only 3 colors, i.e. the block with the div resulting in 0 gets the first color, the block with the result 1 gets the second color, the block with the result 2 gets the third color, but: the block with the result 3 should get color 0 again etc. This means that you must convert the sequence 0,0,0,1,1,1,2,2,3,4,4,4,... to 0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,...

Which operation do you need for this?

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #21 on: December 20, 2017, 03:09:34 pm »
that should be the MOD then
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #22 on: December 20, 2017, 03:13:32 pm »
So the result of the procedure is:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijden_Overzicht.DBGrid_WedstrijdenPrepareCanvas(
  2.   sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);
  3. var b,i: integer;
  4. begin
  5.   i := (Sender as TDBGrid).Datasource.Dataset.RecNo div 5;
  6.   b := i mod 2;
  7.   if b = 0 then
  8.      (Sender as TDBGrid).Canvas.Brush.Color:=clYellow;
  9. end;     // DBGrid_WedstrijdenPrepareCanvas
  10.  


PS: i only needed 2 colors and i wanted it in sets of 5
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #23 on: December 20, 2017, 03:23:01 pm »
But this brings me to the next problem...


If i scroll down i see the sets of 5 i just made and everything goes well.
But when i scroll up with my mouse button i NEVER reach the top because the canvas gets repainted every time.


How to correct that so  can reach the top of the list.
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #24 on: December 20, 2017, 03:25:04 pm »
See the attached example.


put the Test.s2db in the directory /users/yourname/documents/
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

wp

  • Hero Member
  • *****
  • Posts: 13515
Re: Colors in a DBGrid
« Reply #25 on: December 20, 2017, 04:49:10 pm »
Giving up. Not able to compile & run your program, it always crashes the debugger, running outside the IDE it stops with a message box "A reference analysis was sent back by the server" (or so, translated from German).

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #26 on: December 21, 2017, 01:47:47 am »
And if you remove in the Project Inspector all the packages EXCEPT SQDBLaz, FCL and LCL?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

wp

  • Hero Member
  • *****
  • Posts: 13515
Re: Colors in a DBGrid
« Reply #27 on: December 21, 2017, 10:47:44 am »
Finally got it working, no idea how.

Could it be that the RecNo of a dataset begins at 1? --> Subtract 1 from RecNo, and you'll have a group of 5 records at the top.

Code: Text  [Select][+][-]
  1.   i := ((Sender as TDBGrid).Datasource.Dataset.RecNo-1) div 5;

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #28 on: December 21, 2017, 05:02:55 pm »
it could be.
But the problem is still there.
When is scroll down.... i get nicely sets of 5.
BUT when is start scrolling up with my mouse scroll button I never really reach the top off my grid and i get groups of 4 or even groups of 3.


How is that possible?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

wp

  • Hero Member
  • *****
  • Posts: 13515
Re: Colors in a DBGrid
« Reply #29 on: December 21, 2017, 05:30:08 pm »
It is working here, Win 10. Probably a Mac issue, there are several of them. I can't test it, I don't have a Mac.

Just for a test: Your grid is linked to dataset TQ_Wedstrijden. Add an event handler to its OnAfterScroll to repaint the grid:

Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijden_Overzicht.TQ_WedstrijdenAfterScroll(
  2.   DataSet: TDataSet);
  3. begin
  4.   DBGrid_Wedstrijden.Invalidate;  // or: .Repaint;
  5. end;
  6.  
« Last Edit: December 21, 2017, 06:17:01 pm by wp »

 

TinyPortal © 2005-2018