Coloring StringGrid from database lazarus / 10-Designer ---------------------------------------------------------------------- Stygian · 2016-09-01 11:41 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 · 2016-09-01 11:52 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 · 2016-09-01 11:54 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1PrepareCanvas(Sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);begin  field: TField;  grid: TDBGrid;begin  if (Sender is TDBGrid) then begin    grid := TDBGrid(Sender);    field := grid.Datasource.Dataset.FieldByName('the_name_of_the_color_field');  grid.Canvas.Brush.Color := field.AsInteger;    // you may also have to adjust the text color (black text on black doesn't look good ;-)    if field.AsInteger > 3*128 then   // bright background --> black text    grid.Canvas.Font.Color := clBlack    else                            // dark background --> white text      grid.Canvas.Font.Color := clWhite;  end;end; ---------------------------------------------------------------------- Stygian · 2016-09-01 12:36 May i ask when PrepareCanvas execute? After each induvidual rowcount incrase? QuoteIs 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 · 2016-09-01 12:49 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 · 2016-09-01 13:45 Quote from: Stygian on September 01, 2016, 02:36:36 pmMay 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 · 2016-09-05 13:15 Dear Everyone, I dumped the my Stringgrid and changed everything on DBGrid. Now it's working fine. Thanks everyone for the help, Sty