如何根据表值对 cxgrid 进行着色?

发布于 2024-11-08 23:23:47 字数 228 浏览 0 评论 0原文

我希望所有存在特定字段名称“hello”的行都被着色 绿色的。我在 customdrawcell 上尝试过:

if abstable1.fieldbyname('somename').asstring = 'Hello' then
  cxgrid.canvas.brush.color:=clGreen

但它不起作用......我在这里缺少什么?

I would like all rows where in particular field name 'hello' is present to get colored
green. I tried this on customdrawcell:

if abstable1.fieldbyname('somename').asstring = 'Hello' then
  cxgrid.canvas.brush.color:=clGreen

But it wont work... what am I missing here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

话少情深 2024-11-15 23:23:47

对单个列或网格对象使用 OnGetContentStyle 事件。使用样式比弄乱画布要容易得多。

Use the OnGetContentStyle event for either individual columns or the grid object. Styles are much easier to work with than messing with the canvas.

黑色毁心梦 2024-11-15 23:23:47

您需要查看每个视图行的内部数据,而不是表中当前位置的数据。还可以使用 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;

You need to look at the internal data for each view row rather that the data of the current position in the table. Also make use of the canvas provided in the OnCustomDrawCell() event.

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;
偷得浮生 2024-11-15 23:23:47

不要尝试更改网格中的画布颜色。相反,我发现这总是是正确的--在View的OnDrawCell处理程序中更改颜色,如下例所示:

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;

cxGrid只是视图的容器。风景是所有绘画发生的地方。
s

Don't try to change canvas colors in the Grid. Rather--and I find this to always be true--change colors in the View's OnDrawCell handler, as in this example:

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;

The cxGrid is just a container for Views. Views are where all the painting occurs.
s

丢了幸福的猪 2024-11-15 23:23:47
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;
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;
長街聽風 2024-11-15 23:23:47

这是我的一个程序中的一些工作代码,它执行类似的操作。

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 是网格,qDockets 是网格中显示的查询。根据显示的值,某些列可能会以不同的颜色绘制,并且在一种情况下 (qDocketsUrgent = 1),整行的背景颜色都会更改。

Here's some working code from a program of mine which does something similar.

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 is the grid, and qDockets is the query being displayed within the grid. Certain columns may be drawn in a different colour depending on the value being displayed, and in one case (qDocketsUrgent = 1), the entire line's background colour is changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文