tms grid change row color depent on datefield valu

How to change the color of a row (bground color) depend on the value of a field in this row.

for example with a string compare, change color to red.

should i do it on getCellLAyout? How to get the correspoinding data?
Or  should i use the DBLlinktoGrid? or TMSFMXBINDDBGRIDLINK

this is something i tried: (WHERE CDSsbalhISLOCKED is a stringfieldfrom my clientdataset)

procedure TBKH9140Form.TMSFMXGrid1GetCellLayout(Sender: TObject; ACol, ARow: Integer; ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
    if CDSsbalhISLOCKED.Value='Y' then
      ALayout.Fill.Color := claRed;

Extrea:Is it possible to link the grid visually using a FMXgridlink? when i want to link my columns visually it will remove the fmxlink and replace with a normal DBgridlink, wich works fine for all of our cases.

greetings and thnx in advance

Dear Mr. Vercruysse, 

The OnGetCellLayout should be sufficient, are you experiencing difficulties with this event?
You can visually link the grid in XE3, with the livebindings wizard.

Kind Regards, 
Scheldeman Pieter

When using the following code

procedure TBKH9140Form.TMSFMXGrid1GetCellLayout(Sender: TObject; ACol, ARow: Integer; ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
 if ( CDSsbalhISLOCKED.Value='J') AND (aRow>=TMSFMXGrid1.FixedRows) AND aCellstate=TCellState.csNormal) then begin
    ALayout.Fill.Color := claRed;

This code works, but has some errors.
When i select a row (SelectionMode=smSingleRow) with ISLOCKED value of 'J', and then select another row, both that row, the previous selected row and the last row change colour to claRed

This also doesnt work after i refresh my dataset, must first select the row foritto change.

If you refresh your dataset you need to call TMSFMXGrid1.BeginUpdate and TMSFMXGRid1.EndUpdate for a visual update. After selection, the selected row has a selected cell state. The OnGetCellLayout is also only called for the rows which need an update. You can use the OnSelectCell event and force a full repaint with TMSFMXGrid1.UpdateGridCells.

Kind Regards, 

Maybe you miss understand the question because if i use the following code it works

  if TMSFMXGrid1.AllCells[2,aRow]='J' then begin
    ALayout.Fill.Color := claRed; 

Where the 3rd Column has width 0. so how would i do this without having to add a non visible column linked to the field i want to check?

if i would use the previous code, and Updategridcells, it could check the same record of my dataset for every cell.

Thnx for the answers.
I'm starting to see the flaws in my way of working.
if I link 2 fields of my dataset to a TMSgrid, there is really no way for the grid to compare to the other firlds of my dataset. while the the old VCL grid had a datasource property. wich made it easy.

Do i need to link the clientdataset record nummer with the grid selected field?

Do you use LiveBindings to link data to the grid or did you manually add the data?


I linked the grid with a datasource (clientdataset) using a LinkGridToDataSource . i only need to view 2 fields from my dataset in the grid, so i only linked these 2 fields.

I need the value from a third field to determine to colour of the row.

thnx for your patience and answers!


You need to access the third field by accessing the Datasource property on the LinkGridToDataSource component added after you have bound your first 2 fields. As a quick test you could directly look in the Dataset and fetch the value for a specific row number:

  (LinkGridToDataSource1.DataSource as TBindSourceDB).DataSet.RecNo := 10;
  (LinkGridToDataSource1.DataSource as TBindSourceDB).DataSet.FieldByName('Field 3').AsString;

If the test works as expected then load the values in an array with first next statements.

The Record number would be the Row number. I suggest you could preload the values as the OnGetCellLayout will be called multiple times for multiple cells.

Kind Regards, 
Scheldeman Pieter.