Recent

Author Topic: Coloring StringGrid from database  (Read 4374 times)

Stygian

  • Jr. Member
  • **
  • Posts: 90
Coloring StringGrid from database
« on: September 01, 2016, 01:41:58 pm »
Hello,

I'm looking for a method to color a stringgrid from database. Everytime when a user set a color it will be saved into the database and next time he/she start the application the coloring also loaded.

I cannot load the database into an array. It will be more than 10k rows. Now it's small but it's growing day by day.

Thanks,
Sty

balazsszekely

  • Guest
Re: Coloring StringGrid from database
« Reply #1 on: September 01, 2016, 01:52:32 pm »
Is there any reason not to use a TDBGrid? Anyway you have the OnDrawCell event for coloring. Just use the forum search, I'm sure somebody already implemented a similar functionality.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Coloring StringGrid from database
« Reply #2 on: September 01, 2016, 01:54:00 pm »
Use the OnPrepareCanvas event of the DBGrid (I assume that you are talking of a DBGrid, not of a StringGrid - but with the StringGrid the solution would follow the same idea). Set the Brush.Color of the canvas according to the value of the db field:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1PrepareCanvas(Sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);
  2. begin
  3.   field: TField;
  4.   grid: TDBGrid;
  5. begin
  6.   if (Sender is TDBGrid) then begin
  7.     grid := TDBGrid(Sender);
  8.     field := grid.Datasource.Dataset.FieldByName('the_name_of_the_color_field');
  9.     grid.Canvas.Brush.Color := field.AsInteger;
  10.     // you may also have to adjust the text color (black text on black doesn't look good ;-)
  11.     if field.AsInteger > 3*128 then   // bright background --> black text
  12.       grid.Canvas.Font.Color := clBlack
  13.     else                              // dark background --> white text
  14.       grid.Canvas.Font.Color := clWhite;
  15.   end;
  16. end;

Stygian

  • Jr. Member
  • **
  • Posts: 90
Re: Coloring StringGrid from database
« Reply #3 on: September 01, 2016, 02:36:36 pm »
May i ask when PrepareCanvas execute? After each induvidual rowcount incrase?

Quote
Is there any reason not to use a TDBGrid?
I'm using a TTimer which refresh an andon screen from the same database in every 5 sec. And i cannot find an option for Manual Refresh in DBGrind.

Regards,
Sty

balazsszekely

  • Guest
Re: Coloring StringGrid from database
« Reply #4 on: September 01, 2016, 02:49:45 pm »
Quote
@Stygian
I'm using a TTimer which refresh an andon screen from the same database in every 5 sec. And i cannot find an option for Manual Refresh in DBGrind.
Just refresh/reopen the dataset(Query, Table, etc...)

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Coloring StringGrid from database
« Reply #5 on: September 01, 2016, 03:45:25 pm »
May i ask when PrepareCanvas execute? After each induvidual rowcount incrase?
Whenever the grid is repainted. I don't know if the increase of RowCount triggers a repaint of the grid, just call DBGrid1.Invalidate if it does not.

Stygian

  • Jr. Member
  • **
  • Posts: 90
Re: Coloring StringGrid from database
« Reply #6 on: September 05, 2016, 03:15:09 pm »
Dear Everyone,

I dumped the my Stringgrid and changed everything on DBGrid. Now it's working fine.

Thanks everyone for the help,
Sty

 

TinyPortal © 2005-2018