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); begin if CDSsbalhISLOCKED.Value='Y' then ALayout.Fill.Color := claRed; end;
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.
if ( CDSsbalhISLOCKED.Value='J') AND (aRow>=TMSFMXGrid1.FixedRows) AND aCellstate=TCellState.csNormal) then begin ALayout.Fill.Color := claRed; end;
end;
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
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.
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?
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.
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.