Como faço para colorir um cxgrid com base no valor da tabela?
-
08-09-2020 - |
Pergunta
Gostaria de todas as linhas onde, em particular, o nome do campo 'Hello' está presente para ser verde colorido.Eu tentei isso customdrawcell
:
if abstable1.fieldbyname('somename').asstring = 'Hello' then
cxgrid.canvas.brush.color:=clGreen
Mas não vai funcionar...O que estou perdendo aqui ?
Solução
Não tente alterar as cores da tela na Grade.Em vez disso - e acho que isso sempre seja verdadeiro - mude as cores no VisualizarO manipulador OnDrawCell de, como neste exemplo:
procedure T_fmTabSnapList.View1CustomDrawCell(Sender: TcxCustomGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if abstable1.fieldbyname('somename').asstring = 'Hello' then
ACanvas.Brush.Color := clGreen
end;
O cxGrid é apenas um contêiner para Views.As vistas são onde toda a pintura ocorre.é
Outras dicas
Use o evento OnGetContentStyle para colunas individuais ou para o objeto de grade.Os estilos são muito mais fáceis de trabalhar do que mexer na tela.
Você precisa examinar os dados internos de cada linha da visualização, em vez dos dados da posição atual na tabela.Use também a tela fornecida no evento OnCustomDrawCell().
procedure TForm1.YourViewCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if(AViewInfo.GridRecord.Values[YourColumn.Index] = 'Hello') then
ACanvas.Brush.Color := clGreen;
end;
procedure Tfm1.Grid1StylesGetContentStyle(Sender: TcxCustomGridTableView;
ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
out AStyle: TcxStyle);
Var
Style1: TcxStyle;
begin
if AItem = nil then exit;
if ARecord.Values[Grid1Med.Index] = true then
AStyle := cxStyleMed;
if ARecord.Values[Grid1CP.Index] = true then
AStyle := cxStyleHost;
if (ARecord.Values[Grid1Med.Index] = true) and
(ARecord.Values[Grid1CP.Index] = true) then
AStyle := cxStyleHostAndCP;
if not VarIsNull(ARecord.Values[colColor.Index]) then
begin
if not Assigned(AStyle) then
AStyle := TcxStyle.Create(Sender);
AStyle.Color := ARecord.Values[colColor.Index];
end;
end;
Aqui está um código funcional de um programa meu que faz algo semelhante.
procedure TDoDockets.grDocketsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
with grDockets do
begin
if (qDocketsOpenCost.asinteger > 1) and (datacol = 5)
then canvas.font.color:= clRed;
if datacol = 9 then // status
if qDocketsColour.Value <> 0
then Canvas.font.color:= tcolor (qDocketsColour.Value);
if datacol = 10 then // attention
if qDocketsAttention.asinteger = 1
then canvas.brush.color:= clRed;
if qDocketsUrgent.asinteger = 1 then canvas.brush.color:= 10092543;
// light yellow
DefaultDrawColumnCell (Rect, DataCol, Column, State);
end
end;
grDockets é a grade e qDockets é a consulta exibida na grade.Certas colunas podem ser desenhadas em uma cor diferente dependendo do valor exibido e, em um caso (qDocketsUrgent = 1), a cor de fundo de toda a linha é alterada.