Recent

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

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[Solved] Colors in a DBGrid [Yes Really]
« on: December 14, 2017, 12:31:56 pm »
Is it possible to have color columns in a DBGrid?
In my example where the 'X' is?
« Last Edit: December 23, 2017, 12:40:19 am by madref »
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

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Colors in a DBGrid
« Reply #1 on: December 14, 2017, 01:03:27 pm »
I haven't tried but I think it is possible. Because TDBGrid has OnPrepareCanvas event, which can be used to change its color when painting.

See my example on TStringGrid here, maybe you can adapt it to TDBGrid:
http://forum.lazarus.freepascal.org/index.php/topic,37181.msg249361.html#msg249361

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: Colors in a DBGrid
« Reply #2 on: December 14, 2017, 04:14:35 pm »
Is it possible to have color columns in a DBGrid?
In my example where the 'X' is?
I don't understand: You want to colorize individual columns, but the 'X's are in individual cells.

For the entire columns you can adjust the Color property of each TColumn. For the individual cells you should follow Handoko's post and use the OnPrepareCanvas event in which you can specify any Canvas properties dependant on data values.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #3 on: December 19, 2017, 12:33:55 pm »
When i try to use this
Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijden_Overzicht.DBGrid_WedstrijdenPrepareCanvas(
  2.   sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);
  3. begin
  4.  
  5.  
  6. end;    
I get an internal error 200611031


What is that???


[Found out why !!!]
i was doing this in another unit and it's completely 100% Delphi but not Lazarus.
Code: Pascal  [Select][+][-]
  1. function SingleQuotedStr (St: String) : String;
  2. begin
  3.   SingleQuotedStr := Chr(39)+ St + Chr(39);
  4. end;    // SingleQuotedStr  


and it should be:
Code: Pascal  [Select][+][-]
  1. function SingleQuotedStr (St: String) : String;
  2. begin
  3.   Result := Chr(39)+ St + Chr(39);
  4. end;    // SingleQuotedStr  
« Last Edit: December 25, 2017, 11:47:44 am by madref »
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

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Colors in a DBGrid
« Reply #4 on: December 19, 2017, 01:10:53 pm »
Can you provide the whole compilable code for us to test? If you're not willing to publicize the project, you can write a sample project that show the error.

I put this code below on my test project, I didn't get the problem like yours:
Code: Pascal  [Select][+][-]
  1. procedure TfrmProduk.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;
  2.   Column: TColumn; AState: TGridDrawState);
  3. begin
  4.   //
  5. end;

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #5 on: December 19, 2017, 02:40:15 pm »
Hmmm..... this is really strange.


I have made a skeleton version of my database so i could put it up here so that you all can try and solve my problem.
But while doing so i found the solution myself with the following code:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijden_Overzicht.DBGrid_WedstrijdenPrepareCanvas(
  2.   sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);
  3. begin
  4.   // Highlight selected column's header with green
  5.     if (DataCol = 12) then
  6.       if Column.Field.AsString = 'x' then
  7.       (sender as TDBGrid).Canvas.Brush.Color := clGreen;
  8. end;
  9.  


Now i am putting it in my original database and now i get this stupid error.
And the error doesn't point to my unit/form that i am using but to a helping unit
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

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #6 on: December 19, 2017, 02:43:47 pm »
Even stanger....
I deleted my Lib-directory, recompiled everything and now its working

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

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: [Solved] Colors in a DBGrid
« Reply #7 on: December 19, 2017, 02:56:37 pm »
Glad to hear your issue has been solved.

Me too ever had problems that the code didn't work as it should be, simply deleting the lib folder and recompiling solved it.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #8 on: December 20, 2017, 09:30:49 am »
I have put this back to [Unsolved] because i want to know if it is also possible to give colors to individual ROWS in a DBGrid?
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

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Colors in a DBGrid
« Reply #9 on: December 20, 2017, 09:37:25 am »
I haven't tested it on TDBGrid, but int TStringGrid it is possible:

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.StringGrid1PrepareCanvas(sender: TObject; aCol,
  2.   aRow: Integer; aState: TGridDrawState);
  3. begin
  4.   if not(Sender = StringGrid1) then Exit;
  5.   if (aRow = 2) then StringGrid1.Canvas.Brush.Color := clYellow;
  6. end;
« Last Edit: December 20, 2017, 09:39:52 am by Handoko »

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #10 on: December 20, 2017, 10:03:22 am »
Yes but DBGrid has no aRow.


Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijden_Overzicht.DBGrid_WedstrijdenPrepareCanvas(
  2.   sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);
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

paweld

  • Hero Member
  • *****
  • Posts: 991
Re: Colors in a DBGrid
« Reply #11 on: December 20, 2017, 10:56:08 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm_Wedstrijden_Overzicht.DBGrid_WedstrijdenPrepareCanvas(
  2.   sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);
  3. begin
  4.   //For every third row
  5.   if (Sender as TDBGrid).Datasource.Dataset.RecNo mod 3=0 then
  6.   (Sender as TDBGrid).Canvas.Brush.Color:=clYellow;
  7.   //When 'x' value in 5 data field
  8.   if (Sender as TDBGrid).Datasource.Dataset.Fields[5].AsString='x' then
  9.   begin
  10.     (Sender as TDBGrid).Canvas.Font.Style:=[fsBold];
  11.     (Sender as TDBGrid).Canvas.Brush.Color:=clGreen;
  12.   end;
  13. end;
  14.  
Best regards / Pozdrawiam
paweld

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #12 on: December 20, 2017, 12:51:39 pm »
But what if i want to have 3 rows of white and then 3 rows of yellow and then 3 rows of white, etc, etc?
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: 11910
Re: Colors in a DBGrid
« Reply #13 on: December 20, 2017, 12:55:25 pm »
Hey come on. Use your brain! Look at paweld's solution. 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?

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Colors in a DBGrid
« Reply #14 on: December 20, 2017, 01:12:31 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
« Last Edit: December 20, 2017, 01:14:46 pm by madref »
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

 

TinyPortal © 2005-2018