Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5348
  • Last Modified:

how to change color of ROW based on record value in dbgrid

I have a grid that lists both workorders and pending workorders. The only difference in the recordset is the length of 'wonum'... if length = 4 it is a pending workorder, if it is 5 then it is a workorder. I want the grid, on show, to color the entire row red for all records where length(wonum) = 4.

Thanks!!!
0
Bianca
Asked:
Bianca
  • 5
  • 3
  • 3
  • +4
1 Solution
 
cyberkiwiCommented:

procedure TForm1.DBGridDrawColumnCell(
   Sender: TObject;
   const Rect: TRect;
   DataCol: Integer;
   Column: TColumn;
   State: TGridDrawState) ;
 var
   grid : TDBGrid;
 begin
   grid := sender as TDBGrid;
 
   if Length(grid.DataSource.DataSet.FieldByName('wonum').AsString) = 4 then
     grid.Canvas.Brush.Color := clRed
   end;
   grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
 
 end; (* DBGrid OnDrawColumnCell *)

Open in new window

0
 
cyberkiwiCommented:
Removed one extra "end;"
procedure TForm1.DBGridDrawColumnCell(
   Sender: TObject;
   const Rect: TRect;
   DataCol: Integer;
   Column: TColumn;
   State: TGridDrawState) ;
 var
   grid : TDBGrid;
 begin
   grid := sender as TDBGrid;
 
   if Length(grid.DataSource.DataSet.FieldByName('wonum').AsString) = 4 then
     grid.Canvas.Brush.Color := clRed;
///   end;
   grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
 
 end; (* DBGrid OnDrawColumnCell *)

Open in new window

0
 
aflarinCommented:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field:
    TField; State: TGridDrawState);
begin
  if Length((Sender as TDBGrid).DataSource.DataSet.FieldByName('wonum').AsString) = 2 then
  begin
    (Sender as TDBGrid).Canvas.Brush.Color := clRed;
    (Sender as TDBGrid).DefaultDrawDataCell(Rect, Field, State);
  end;
end;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
8080_DiverCommented:
Cyberwiki,
Just out of curiosity (because I haven't played with this sort of thing much), does the grid.Canvas.Brush.Color revert to the default color each time the TForm1.DBGridDrawColumnCell procedure is called?
0
 
aflarinCommented:
cyberkiwi, you're faster... but I have had time to check up my solution ;)
0
 
cyberkiwiCommented:
8080_Diver,

Caught me there - not sure and can't test right now!

aflarin,

I didn't have Delphi open - but curious,

Does it actually draw without calling DefaultDrawDataCell?
Also, the len(wonum) needs to be 4, not 2! :)
0
 
8080_DiverCommented:
cyberwiki,
Hmmm, since aflarin has had time to test his (even if it was with a length of 2 ;-), perhaps he can answer my question. ;-)
0
 
aflarinCommented:
>> Does it actually draw without calling DefaultDrawDataCell?

Yes, it draws without calling DefaultDrawDataCell. I decided to remove double drawing when found out it.

>> Also, the len(wonum) needs to be 4, not 2! :)

You're right :) I'm sorry, Prec
0
 
cyberkiwiCommented:
So I can dock the defaultdraw in the If statement.

Aflarin, if you would oblige, the other question is why DataCell not ColumnCell?


procedure TForm1.DBGridDrawColumnCell(
Sender: TObject; const Rect: TRect;DataCol: Integer;Column: TColumn;State: TGridDrawState) ;
 var
   grid : TDBGrid;
 begin
   grid := sender as TDBGrid;
 
   if Length(grid.DataSource.DataSet.FieldByName('wonum').AsString) = 4 then
   begin
     grid.Canvas.Brush.Color := clRed;
     grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
   end;
 
 end; (* DBGrid OnDrawColumnCell *)
0
 
BiancaAuthor Commented:
Those did not seem to work with the grid I am using which is a TcxDBGrid. Here is what comes up for the OnCustomDrawCell event:

procedure TfrmWorkOrder.cxGrid1DBTableViewWorkOrderCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;

end;
0
 
cyberkiwiCommented:
procedure TfrmWorkOrder.cxGrid1DBTableViewWorkOrderCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if  Length(VarToStr(AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('wonum').Index])) = 4 then
    ACanvas.Brush.Color := clRed;
end;

0
 
bjoveCommented:
Replace cxViewWonum in the code below with actual name of that column in cxView.
procedure TfrmWorkOrder.cxGrid1DBTableViewWorkOrderCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  val: string;
begin
  val := VarAsType(AViewInfo.GridRecord.DisplayTexts[cxViewWonum.Index], varString);
  if Length(val) = 4 then ACanvas.Canvas.Brush.Color := clRed; 
end;

Open in new window

0
 
Ephraim WangoyaCommented:
procedure TfrmWorkOrder.TableCustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  AValue: Variant;
begin
   //Row level
  AValue := AViewInfo.GridRecord.Values[YourColumn.Index];
  if not IsNull(AValue) then
  begin
    if Length(AValue) = 4 then
      ACanvas.Canvas.Font.Color := clRed; //this colors the text
      //ACanvas.Canvas.Brush.Color := clRed; //this will color the entire cell
  end;
end;
0
 
Ephraim WangoyaCommented:
Here is my function for IsNull

function IsNull(const AValue: Variant): Boolean;
begin
  Result := VarIsNull(AValue) or VarIsEmpty(AValue) or VarIsClear(AValue);
end;
0
 
Geert GruwezOracle dbaCommented:
looks like above there are comments enough for colouring rows :)

only 2 colors and only 2 states for workorders ?
we use a extra column in the table of the workorders "Status"

it's just a number.

We also have a additional table OrderStatus
This contains that status number and the color it must show
Later on we added a new column (userid)
Some users were colorblind, so we had to help them with customizable colors

Some time ago a new status was invented ... REWORK
We didn't have to change our code, for this new color, only add a record in the table
And change the status changing procedure a little in the db too

For the future it would save you a lot of work if using a extra status and extra table
At first don't give the users the opportunity to customize the colors
Because they will want that straight away.
Later you can add for admins to change the colors.

You can add a lot more information using such a status table later on
Sample of such a table
MTYPE	STATEID	CODE	DESCRIPTION	COLOR	R_COLOR	G_COLOR	B_COLOR	NSTATES	INFO	     DESCRIPTION_NL
2	110	RES	RESERVED	GREY	175	185	200		RESERVE	     GERESERVEERD
2	140	ACT	ACTIVE	        GREEN	20	255	170	160	BUSY	     ACTIEF
2	150	CCL	CANCELED	RED	255	20	100	170	INTERRUPT    AFGELAST
2	160	FIN	FINISHED	BLUE	122	128	255	170	FINISHED     BEËINDIGD
2	120	REQ	REQUESTED	YELLOW	250	250	120		RESERVE	     IN AANVRAAG
1	100	PLN	PLANNED	        WHITE	220	230	230	140	RESERVE	     GEPLAND
3	100	PLN	PLANNED	        WHITE	220	230	230	110	RESERVE	     GEPLAND
3	110	RES	RESERVED	GREY	175	185	200	150,160	RESERVE	     GERESERVEERD
3	140	ACT	ACTIVE	        GREEN	20	255	170	150,160	BUSY	     ACTIEF
3	150	CCL	CANCELED	RED	255	20	100	170	INTERRUPT    AFGELAST
3	160	FIN	FINISHED	BLUE	122	128	255	170	FINISHED     BEËINDIGD
3	170	CLO	CLOSED	        NONE	20	180	255			     AFGEWERKT
3	120	REQ	REQUESTED	YELLOW	250	250	120	150,160	RESERVE	     IN AANVRAAG
2	100	PLN	PLANNED	        WHITE	220	230	230	140	RESERVE	     GEPLAND
1	140	ACT	ACTIVE	        GREEN	20	255	170	150,160	BUSY	     ACTIEF
1	150	CCL	CANCELED	RED	255	20	100	170	INTERRUPT    AFGELAST
1	160	FIN	FINISHED	BLUE	122	128	255	170	FINISHED     BEËINDIGD
1	170	CLO	CLOSED	        NONE	20	180	255			     AFGEWERKT
2	170	CLO	CLOSED	        NONE	20	180	255			     AFGEWERKT

Open in new window

0
 
Geert GruwezOracle dbaCommented:
it's a cxGrid ?
No use with CustomDraw

Don't do colors like that.
You need to use the stylesheets of the table for that to set a color for the row

You need to add a StyleRepository
Then add 2 styles, 1 with first color (and text formatting) second with other color (and text formatting)
Give each a name and then you can use that name to assign it to AStyle

To have different colors you need to fill in the style event to
this is for my table view Stock
procedure TfrmMainTools.viewStockStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  if ARecord <> nil then
  begin
    if ARecord.Values[colStockAmountFree.Index] < ARecord.Values[colStockTarget_Free.Index] then
      AStyle := cxsStockStateFree;
    if ARecord.Values[colStockAmount_not_worned.Index] < ARecord.Values[colStockAmount_not_worn_max.Index] then
      AStyle := cxsStockStateNearlyWorn;
    if ARecord.Values[colStockAmount_Not_Worned.Index] < ARecord.Values[colStockTarget_Not_Worned.Index] then
      AStyle := cxsStockStateWorn;
  end;
end;

0
 
BiancaAuthor Commented:
Works perfectly!!!
0
 
Geert GruwezOracle dbaCommented:
fwiw, you'll back on that ... :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now