coloring dbgrid (grouped alternated) lazarus / 31-LCL ---------------------------------------------------------------------- bpp · 2020-06-14 20:03 Hi I used this code to colorize a dbgrid: 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 TfrmOPS.DBGridPrepareCanvas(sender: TObject;  DataCol: Integer; Column: TColumn; AState: TGridDrawState);varmyweek: string;begin    myweek:= DBGrid.DataSource.DataSet.FieldByName('Week').AsString;   if StrToInt(myweek) mod 2 = 0 then DBGrid.Canvas.Brush.Color:=$8a6076;end; So.. if the week it's a pair number, they receive a color. The color is alternated by the weeks. But if the user insert in dbgrid only weeks with pair number, they not alternate the color as expected (has we can see in attached pic, number 18 and 20) I'm trying to use some conditions and Boolean variables, but whitout success. any ideias? thanks ---------------------------------------------------------------------- wp · 2020-06-16 21:40 I do not understand. From your code I would expect that there should be two colors: the standard Color of the grid, and the color $8a6076 to be used for all rows with even "Week" values. But your screenshot displays many more colors... ---------------------------------------------------------------------- TRon · 2020-06-16 21:51 @wp I do believe that is an old-fashioned screenshot taken with a camera.Seems that the lighting is a bit wonky, but if you zoom in and look hard you are perhaps also able to distinguish two different colors. ---------------------------------------------------------------------- bpp · 2020-06-16 21:56 If the week is a pair number “(myweek) mod 2 = 0” the color $8a6076 Will be display. But the problem detected, it’s when the user insert two pair numbers like we saw in picture. this is what i want: when the week number change, the color should be alternated In the picture case: 16 painted 16 painted 17 not painted 17 not painted 18 painted 18 painted 20 not painted 20 not painted ---------------------------------------------------------------------- TRon · 2020-06-16 22:02 ... and stupid me just noticed that the picture is exactly showing what the code is doing, even numbered weeks one color odd numbered weeks the other. edit: question to TS removed as TS already wrote what was expected ---------------------------------------------------------------------- wp · 2020-06-16 22:14 So there will be always two rows with the same week number? This is difficult because it is not known that the currently painted row is the first or second one of the pair... I think it would work to populate a StringGrid with the table where you have full access to all cells of the grid and easily can tell whether a row is the first one of a pair or not. Or: You could add a hidden auxiliary field to the dataset which contains the color values. You must iterate once through the entire dataset and store the corresponding color values. Both solutions are not very practical when there are many (millions) rows. How big is the dataset, i.e. how many records does it have? ---------------------------------------------------------------------- bpp · 2020-06-16 22:21 sorry my bad! new pic attached. @TRon exaclty! the code is doing what is expected. But what I need it's alternate the color when the number of the week change! so the "pair number" solution not work for me. @wp No, the user can insert a lot of weeks 5000 rows per year expected ---------------------------------------------------------------------- TRon · 2020-06-16 22:25 Quote from: bpp on June 17, 2020, 12:21:42 amexaclty! the code is doing what is expected. But what I need it's alternate the color when the number of the week change! so the "pair number" solution not work for me. Yes, sorry bpp. I was too slow  you had already written that you expected something else. If the color only needs to change, when the week number differs from the last one, then would the following work ? 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 var  LatestWeek : integer = 0;  WantOtherColor : boolean = false; procedure TfrmOPS.DBGridPrepareCanvas(sender: TObject;  DataCol: Integer; Column: TColumn; AState: TGridDrawState);varmyweek: string;begin    myweek:= DBGrid.DataSource.DataSet.FieldByName('Week').AsString;     WantOtherColor := myweek.ToInteger <> LatestWeek;    If WantOtherColor then    begin       LatestWeek := myweek.ToInteger;      DBGrid.Canvas.Brush.Color:=$8a6076;    end;end;  I'm positive my logic is flawed somewhere along the line, but something like that ? I have no idea how that works with regards to the grid itself (redrawing, whole page distance etc). ---------------------------------------------------------------------- winni · 2020-06-16 22:41 Hi! The easiest way is like you did it: The odd weeks get one color and the even weeks get another one. In the example it comes to the point that week 18 and week 20 got the same color and there is no week 19 inbetween. If this is a problem you could work with 3 colors: 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 var color : TColor ;...case week mod 3 of0: color := clLime;1: color := clAqua;2: color := clYellow;end; // caseDBGrid.Canvas.Brush.Color:=color;... Winni ---------------------------------------------------------------------- wp · 2020-06-16 23:01 Here is a simple project demonstrating the idea with the auxiliary color field. Week numbers are created by a random number generator, and for every week two records are added to the dataset. As you scroll down the grid you see that the color changes always when the week number changes. You'll see sometimes groups of 4 rows having the same color - this is when the random generator has picked the same week number twice. ---------------------------------------------------------------------- TRon · 2020-06-16 23:58 oh, uhm  i am indeed flawed 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 var  LatestWeek: integer= 0;  WantOtherColor: boolean= false; procedure TfrmOPS.DBGridPrepareCanvas(sender: TObject;  DataCol: Integer; Column: TColumn; AState: TGridDrawState);varmyweek: string;begin    myweek:= DBGrid.DataSource.DataSet.FieldByName('Week').AsString;     if myweek.ToInteger<>LatestWeek then    begin      LatestWeek:= myweek.ToInteger;      WantOtherColor:= not(WantOtherColor);    end;     if WantOtherColor then DBGrid.Canvas.Brush.Color:=$8a6076;end; ---------------------------------------------------------------------- Lutz Mändle · 2020-06-17 09:33 For a zebra effect the property AlternateColor of the dbgrid can used, then no special coding is needed (see attached screenshot). ---------------------------------------------------------------------- bpp · 2020-06-17 17:26 @TRon It's a good approach, I have tried something like that, but I had the same issue as your code! When you click a row after row, the colors will be alternated. @wp I don't know why, but I got this error when I'm trying to compile Error: Identifier idents no member "ColumnByFieldname" @winni Works like you describe, it's a good approach too! It Will be my choice if the first one still fail... ---------------------------------------------------------------------- TRon · 2020-06-17 19:22 Quote from: bpp on June 17, 2020, 07:26:00 pm@TRon It's a good approach, I have tried something like that, but I had the same issue as your code! When you click a row after row, the colors will be alternated. Also with the revised code at post #10 ? ---------------------------------------------------------------------- bpp · 2020-06-17 19:54 Yes, you can see what I'm say in the pics. a little test: - the value of "WantOtherColor" for the last row (with value 15) show's seven true's (one true per cell, cus they are 7 cells per row) - if I click in above row with value 13, the variable "WantOtherColor" show me only false values (seven falses) for the row with value 15 insteded of true values. As expected this alternates the color ---------------------------------------------------------------------- TRon · 2020-06-17 20:20 Quote from: bpp on June 17, 2020, 09:54:50 pmYes, you can see what I'm say in the pics. For what it is worth, that is what my code intended to accomplish. In that regards it works  I had the assumption that such color switching scheme was what you wanted, e.g.: every time the week number changes the color needs to switch. Quotea little test: - the value of "WantOtherColor" for the last row (with value 15) show's seven true's (one true per cell, cus they are 7 cells per row) - if I click in above row with value 13, the variable "WantOtherColor" show me only false values (seven falses) for the row with value 15 insteded of true values. As expected this alternates the color I am sorry because i am unable to grasp how and when exactly you wish for the color(s) to switch. ---------------------------------------------------------------------- wp · 2020-06-17 20:35 Quote from: bpp on June 17, 2020, 07:26:00 pm@wp I don't know why, but I got this error when I'm trying to compile Error: Identifier idents no member "ColumnByFieldname" Sorry. I work with Laz trunk and was not aware that this function is not available in current release versions. You can use the following function instead: 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 function ColumnByFieldName(ADBGrid: TDBGrid; AFieldName: String): TColumn;var  col: TCollectionItem;begin  for col in ADBGrid.Columns do    if TColumn(col).FieldName = AFieldName then    begin      Result := TColumn(col);      exit;    end;  Result := nil;end; // at the end of FormCreate:...  BufDataset1.Open;  PopulateColorField;  //DBGrid1.Columns.ColumnByfieldName('Color').Visible := false;     // <--- replace by new function  ColumnByFieldname(DBGrid1, 'Color').Visible := false;  end; ---------------------------------------------------------------------- bpp · 2020-06-17 20:50 @TRon let me explain it with another example... using the 3 colors scheme like @winni sugest, it works fine (see attached pic) but instead of 3 colors I just want to use two colors. When I use the code sugest by you, in a first approach it works! but fails when the others row are clicked. The scheme change when the row are clicked, and I need to maintain the same scheme... I made this short video:https://youtu.be/7iizzGle9Mk ---------------------------------------------------------------------- wp · 2020-06-17 21:49 Quote from: bpp on June 17, 2020, 10:50:42 pmWhen I use the code sugest by you, in a first approach it works! but fails when the others row are clicked. The scheme change when the row are clicked, and I need to maintain the same scheme... The problem is that grid drawing takes advantage of several optimizations and draws only parts of the client area. When you click in a row, only the previously selected row and the new row are painted (maybe painting is restricted even further to the selected cells onyl). There is no way to tell whether a row is the first or second row of a pair (unlike in TStringGrid allowing random access to any cell even during painting). This is only clear when there is a permanent auxiliary "color" column as used in my sample code above. Another possibility would be to use a helper dataset which looks up the week number in the previous and next records whenever a row is painted. But it can be anticipated that grid drawing will become paintfully slow this way. ---------------------------------------------------------------------- TRon · 2020-06-17 22:39 Quote from: bpp on June 17, 2020, 10:50:42 pmlet me explain it with another example... Thanks. The video let me understand exactly what is going wrong. It is indeed a flaw in my approach, also because of my unfamiliarity with the DbGrid component. Quoteusing the 3 colors scheme like @winni sugest, it works fine (see attached pic) but instead of 3 colors I just want to use two colors. winni's solution makes clever use of a odd/even characteristic, but it is also flawed in case there are 2 (or is it 3?) weeks missing between two rows. QuoteWhen I use the code sugest by you, in a first approach it works! but fails when the others row are clicked. The scheme change when the row are clicked, and I need to maintain the same scheme... See also wp's answer. Thanks to wp's answer (@wp: thank you) i also understand this can't be solved in a 100% correct manner than suggested. My first thought was to try and determine the first row that is being prepared and store that into a variable as well, but if the preparation of a canvas is done in a more or less random fashion, such as indicated by wp then i have no idea how this could be solved other then adding an extra/additional field to the query that would contain the color (or color change). Or as suggested by wp by using a helper dataset. edit: @wp could a TBits perhaps be filled based on the data beforehand ? e.g, by means of a DBGrid helper. ---------------------------------------------------------------------- TRon · 2020-06-18 02:51 Following my own idea with using bits (and using wp's source as a base), this is the best i can do for now and which doesn't require to add an extra field to the table. 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 type  TDBGrid= class(DBGrids.TDBGrid)  private    FBits: TBits;  public    constructor Create(aOwner: TComponent); override;    destructor Destroy; override;    procedure PrepareGroupColorByIntegerField(aFieldname: string);  end; constructor TDBGrid.Create(aOwner: TComponent);begin  inherited Create(aOwner);  FBits:= TBits.Create(0);end; destructor TDBGrid.Destroy;begin  FBits.Free;  inherited Destroy;end; procedure TDBGrid.PrepareGroupColorByIntegerField(aFieldname: string);var  DataSet    : TDataSet;  prevNumber : Integer= 0;  thisNumber : integer= 0;  index      : SizeInt= 0;  GroupBit   : boolean= false;begin  DataSet:= Self.DataSource.DataSet;   DataSet.DisableControls;   DataSet.Open;  try    FBits.Size:= DataSet.RecordCount;    DataSet.First;     while not Dataset.EoF do  begin      thisNumber:= DataSet.FieldByName(aFieldname).AsInteger;    if thisNumber<>prevNumber then      begin        GroupBit:= not(GroupBit);        prevNumber:= thisNumber;      end;       FBits.Bits[Dataset.RecNo]:= GroupBit;      DataSet.Next;      inc(index);    end;  finally    DataSet.Close;    DataSet.EnableControls;  end;end; procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer; Column: TColumn; AState: TGridDrawState);var  dataset: TDataset;begin  dataset := DBGrid1.DataSource.Dataset;  if DBGrid1.FBits.Bits[dataset.RecNo] and (AState * [gdSelected, gdFocused] = [])    then DBGrid1.Canvas.Brush.Color := clMoneyGreen;end;  At form creation instead of the following part 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   BufDataset1.Open;  PopulateColorField;  DBGrid1.Columns.ColumnByFieldname('Color').Visible := false;  ... you can use it like this: 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   DBGrid1.PrepareGroupColorByIntegerField('Week');  BufDataset1.Open;  DBGrid1.Columns.ColumnByFieldname('Color').Visible := false; ---------------------------------------------------------------------- wp · 2020-06-18 07:41 Nice solution. You should keep in mind, though, that the RecNo property is not necessarily implemented for every TDataset descendant. And you also must not forget to update the Bits whenever a record is added, edited (at least the "Week" field) or deleted (the same is required for my auxiliary field solution). The line "DBGrid1.Columns.ColumnByFieldname('Color').Visible := false;", BTW, can be dropped because this column does not exist any more. ---------------------------------------------------------------------- TRon · 2020-06-18 13:08 Quote from: wp on June 18, 2020, 09:41:59 amNice solution. Thank you, also for your feedback. It my first attempt at something like this so I have no idea if it is any good at all. QuoteYou should keep in mind, though, that the RecNo property is not necessarily implemented for every TDataset descendant. Thank you for the heads up, as I didn't know. uhm, yeah ... I forgot to look at the documentation QuoteThis property should not be used if exact positioning is required. it is inherently unreliable. That, I did not know either  Is there a more reliable way ? I am currently not aware of any. As you perhaps noticed there are some remnants left behind in my code using an index, but i could't get that to work reliable as the (direct) mapping/relation between that index and the row/record is missing. QuoteAnd you also must not forget to update the Bits whenever a record is added, edited (at least the "Week" field) or deleted (the same is required for my auxiliary field solution). Yes, that I did know. That's why I was looking how this solution could be integrated in the DBGrid component itself and I got stuck trying to use that approach. What is then the best direction to look at to be able to accomplish this behaviour, at least a bit hidden (helper/overload) and automatically. Should I be looking at TDataset instead ? QuoteThe line "DBGrid1.Columns.ColumnByFieldname('Color').Visible := false;", BTW, can be dropped because this column does not exist any more. Uhm, yes you are right. In the code I worked with, I left your DB creation active so that there was still the 'hidden' Color column (that way I was able to verify my solution against yours  ) Sorry for all the questions. ---------------------------------------------------------------------- wp · 2020-06-18 15:09 Quote from: TRon on June 18, 2020, 03:08:55 pmQuoteThis property should not be used if exact positioning is required. it is inherently unreliable. I am not sure what is meant here exactly. Maybe that the same record can have a different RecNo when the dataset is resorted. I think this is not important to your current code because you always have to recalculate the bits whenever the record sequence changes and RecNo might become invalid. The real problem with RecNo is that it is not necessarily implemented for all dataset types; I've seen TDataset descandants for Delphi where this happened and RecNo always returns -1 (or was this RecordCount? - I don't remember exactly...). The elemental TDataset types of Lazarus on the component palette do have a usable RecNo. But I don't know about TSQLQuery, here it probably depends on the underlying database engine, but I am not sure. Quote from: TRon on June 18, 2020, 03:08:55 pmIs there a more reliable way? I am currently not aware of any [...] I was looking how this solution could be integrated in the DBGrid component itself [...] What is then the best direction to look at to be able to accomplish this behaviour, at least a bit hidden (helper/overload) and automatically. Should I be looking at TDataset instead ? I think that adding such a feature to DBGrid is not the right way to go in general because it might be needed in one way or another also by other visual db controls. Therefore, I believe that it should be made available already by TDataset, maybe like a function "PreviousValue: Variant" of TField. I missed such a feature in another context, namely for calculating cumulative sums of field values (*). A Dataset uses several buffers, and it should be possible to access the fields which belong to the buffer of the previous record, i.e. the one with RecNo-1. TDataset, however is quite complex and certainly it requires a lot of time to understand every detail. And I cannot guarantee whether such a feature will be added at all because it appears very special to me. Your current solution certainly works for the OP (unless he uses a DB with "unreliable" RecNo). ---- (*) I somehow have the feeling that cumulative sums can be calculated by SQL instructions because this is a common task in data analysis - but I am not the expert here. If this is true the SQL instruction could probably give the idea how to solve the pair coloring problem by SQL. ---------------------------------------------------------------------- bpp · 2020-06-18 19:11 @wp @TRon first of all I would like to thank you for the comments and views expressed! I'm trying something different now: I got the same color for all rows in DBgrid, and when some "week" is clicked, all rows with that "week" received a color. So, this will "highlight" all the rows with the same week. 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 TfrmOPS.DBGridPrepareCanvas(sender: TObject;  DataCol: Integer; Column: TColumn; AState: TGridDrawState);varmyweek: string;begin myweek:= DBGrid.DataSource.DataSet.FieldByName('semana').AsString; if myeek.ToInteger = SelWeek then begin DBGrid.Canvas.Brush.Color:=$0097572bend; end;       procedure TfrmOPS.DBGridCellClick(Column: TColumn);begin  selweek:= DBGrid.DataSource.DataSet.FieldByName('week').AsInteger;end; In my case, this is acceptable too. ---------------------------------------------------------------------- wp · 2020-06-18 20:08 Is this working for all combinations of previously selected and clicked cells? I don't think so because the grid should not repaint everything and all rows belonging to the previously selected and clicked week will not necessarily be updated. I think it is safer when you enforce a full repaint of the grid: 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 TfrmOPS.DBGridCellClick(Column: TColumn);begin  selweek:= DBGrid.DataSource.DataSet.FieldByName('week').AsInteger;  DBGrid.Invalidate;  // <--- repaint the entire gridend; I suppose it is a typo in your code to distinguish between fields "week" and "semana". Since the "week" field probably is an Integer field, the OnPrepareCanvas handler can be simplified by reading the week integer directly rather than reading it as a string and converting it back to Integer. 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 TfrmOPS.DBGridPrepareCanvas(sender: TObject;  DataCol: Integer; Column: TColumn; AState: TGridDrawState);begin  if DBGrid.DataSource.DataSet.FieldByName('week').AsInteger = selWeek then  begin   DBGrid.Canvas.Brush.Color:=$0097572b  end;end; And if you want to highlight the selected cell you should check the cell's state: 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 TfrmOPS.DBGridPrepareCanvas(sender: TObject;  DataCol: Integer; Column: TColumn; AState: TGridDrawState);begin  if (DBGrid.DataSource.DataSet.FieldByName('week').AsInteger = selWeek) and     (AState * [gdSelected, gdFocused] = [])  then  begin   DBGrid.Canvas.Brush.Color:=$0097572b  end;end; ---------------------------------------------------------------------- bpp · 2020-06-19 17:46 QuoteI suppose it is a typo in your code to distinguish between fields "week" and "semana". Since the "week" field probably is an Integer field, the OnPrepareCanvas handler can be simplified by reading the week integer directly rather than reading it as a string and converting it back to Integer. yes it is a typo! It's is identification of a field from an old table. QuoteAnd if you want to highlight the selected cell you should check the cell's state: I don't express myself correctly. What i meant, it's all the rows with same "week" will be paint in the same color. Any way, it's a good tip because I will use this too. ---------------------------------------------------------------------- TRon · 2020-06-19 21:35 Thank you very much for the additional feedback on recno wp. Quote from: wp on June 18, 2020, 05:09:33 pmI think that adding such a feature to DBGrid is not the right way to go in general because it might be needed in one way or another also by other visual db controls. Thank you for that valuable feedback, as it would have wasted me a lot of time otherwise. QuoteTherefore, I believe that it should be made available already by TDataset, maybe like a function "PreviousValue: Variant" of TField. I missed such a feature in another context, namely for calculating cumulative sums of field values (*). A Dataset uses several buffers, and it should be possible to access the fields which belong to the buffer of the previous record, i.e. the one with RecNo-1. TDataset, however is quite complex and certainly it requires a lot of time to understand every detail. Again, i thank you because i hadn't thought about looking at the previous record yet. That might indeed proof to be a more clever approach. Now i need to get myself more familiar with the TDataset class, as i did not manage to do so on such short notice. QuoteAnd I cannot guarantee whether such a feature will be added at all because it appears very special to me. Your current solution certainly works for the OP (unless he uses a DB with "unreliable" RecNo). This is purely for my own curiosity, as i like to understand how these things work (if at all). So for me it is all experimental, and indeed the posted code could be a workable solution for TS (... and he seems to have taken another approach). Quote(*) I somehow have the feeling that cumulative sums can be calculated by SQL instructions because this is a common task in data analysis - but I am not the expert here. If this is true the SQL instruction could probably give the idea how to solve the pair coloring problem by SQL. I am at least aware of calculated fields, and SQL does have some instructions that allow for calculations to happen. I have not experimented with them yet. Database is a broad subject, and i've just taken my first steps ---------------------------------------------------------------------- wp · 2020-06-19 22:45 Quote from: TRon on June 19, 2020, 11:35:55 pmQuote(*) I somehow have the feeling that cumulative sums can be calculated by SQL instructions because this is a common task in data analysis - but I am not the expert here. If this is true the SQL instruction could probably give the idea how to solve the pair coloring problem by SQL. I am at least aware of calculated fields, and SQL does have some instructions that allow for calculations to happen. I have not experimented with them yet. Database is a broad subject, and i've just taken my first steps  I found a link about calculating cumulative sums by SQL, https://stackoverflow.com/questions/2120544/how-to-get-cumulative-sum, but I must say, the SQL expressions really are quite scary, and I get the feeling that the pair coloring problem might be even worse (if solvable by SQL at all). ---------------------------------------------------------------------- TRon · 2020-06-20 04:06 Quote from: wp on June 20, 2020, 12:45:38 amI found a link about calculating cumulative sums by SQL, https://stackoverflow.com/questions/2120544/how-to-get-cumulative-sum, but I must say, the SQL expressions really are quite scary, and I get the feeling that the pair coloring problem might be even worse (if solvable by SQL at all). Thank you for the link,which led me to investigate a little further. I haven't been able to verify myself as my version of sql(ite) that comes with my distribution seems too old but the coloring problem should be solvable by using the ("new") introduced window functions, lag() and lead(). see https://www.sqlite.org/draft/windowfunctions.html or perhaps better explained here https://www.sqlitetutorial.net/sqlite-window- functions/sqlite-lag/ ---------------------------------------------------------------------- wp · 2020-06-20 14:43 Here is another (hacky) solution to the original question of how to achieve a banded look such that the band color changes wnenever some field ("week") changes its value. An auximilary column or some other storage can be avoided when every repaint handles the entire control. This way a group variable can updated while painting progesses down the screen. The present solution uses an interposer class for TDBGrid which overrides methods to enforce a full repaint of the eintire control (maybe I forgot some...).  The Paint method resets the FCurrentGroup variable; the user is responsible to maintain it as required which can be done in the OnPreparecanvas event handler: The user stores the number of the previously painted week, and when the week changes the FCurrentgroup is incremented. Then the painting color can be derived whether the FCurrentGroup variable is odd or even. This is simular to what TRon attempted in reply #10 above. The solution is not 100% perfect because the grid's Paint method iterates only through the visible rows and thus the top-most color always has the same color. When the grid is scrolled you may notice that the bands change their color whenever a new group reaches the top. Maybe somebody has an idea how the week of the record before the first visible row could be queried without destroying too much.