0

I would like to apply a style to a column (on get content style) so all numbers in that column color to the selected style. Just the numbers, nothing else.That means that if a number is found within a text phrase it will get colored. Is this possible ?

user763539
  • 3,509
  • 6
  • 44
  • 103
  • possible duplicate of [delphi - coloring cxdbgrid field depending on its content](http://stackoverflow.com/questions/20183431/delphi-coloring-cxdbgrid-field-depending-on-its-content) – Sam M May 26 '14 at 01:36
  • Isn't this the same question you asked earlier? http://stackoverflow.com/questions/20183431/delphi-coloring-cxdbgrid-field-depending-on-its-content?rq=1 – Sam M May 26 '14 at 01:36
  • @SamM similar but not the same....Now I want the numbers found in the text to get colored, leaving the text intact. – user763539 May 26 '14 at 23:02
  • In your style object, what properties are you setting? Posting the object's definition from your dfm file would be very helpful. – Sam M May 27 '14 at 01:59
  • just text color :clred for the style nothing else... – user763539 May 27 '14 at 02:30

2 Answers2

2

Sure. Use something like

procedure TForm1.Column1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
var
  i: Integer;
begin
  if TryStrToInt(ARecord.Values[Column1.Index], i) then
    AStyle := cxStyle1;
end;
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • I tried : if StrToInt('123') then... but [dcc32 Error] Unit3.pas(140): E2012 Type of expression must be BOOLEAN – user763539 May 24 '14 at 15:36
  • if I use . if TryStrToInt('123456',0) then ... I get [dcc32 Error] Unit3.pas(140): E2250 There is no overloaded version of 'TryStrToInt' that can be called with these arguments ... maybe I am just messing up.... – user763539 May 24 '14 at 15:42
  • @user763539, The second parameter of TryStrToInt must be a variable, not a constant. You are passing it a constant (0) which is why you get the compiler error. – Sam M May 24 '14 at 18:20
  • @Sam M How am I supposed to write it ? – user763539 May 24 '14 at 21:07
  • var i:integer; and TryStrToInt(123,i) colors everything in the column. I want just the numbers. – user763539 May 24 '14 at 21:47
0

This is rough code but ought to get you in the right direction. I think it may overlap your drawing a little on the canvas but you can adjust where needed. You will also need to adjust so that it parses out the numbers from the strings.

procedure TForm7.cxGrid1TableView1CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var Bounds : TRect;
begin
  Bounds := AViewInfo.Bounds;

  ACanvas.Font.Color := clRed;
  ACanvas.TextOut(Bounds.Left, Bounds.Top, '123');
  Bounds.Left := ACanvas.Canvas.TextWidth('123');

  ACanvas.Font.Color := clGreen;
  ACanvas.TextOut(Bounds.Left, Bounds.Top, 'abc');

  ADone := True;
end;
Sam M
  • 4,136
  • 4
  • 29
  • 42