Solved

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

Posted on 2010-08-24
18
1,964 Views
Last Modified: 2012-05-10
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
Comment
Question by:Bianca
  • 5
  • 3
  • 3
  • +4
18 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33515105

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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33515119
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
 
LVL 13

Expert Comment

by:aflarin
ID: 33515194
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
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33515232
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
 
LVL 13

Expert Comment

by:aflarin
ID: 33515250
cyberkiwi, you're faster... but I have had time to check up my solution ;)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33515469
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
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33515542
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
 
LVL 13

Expert Comment

by:aflarin
ID: 33515551
>> 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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33515617
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Bianca
ID: 33515681
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
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33515887
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
 
LVL 4

Expert Comment

by:bjove
ID: 33515935
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 33517204
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 33517472
Here is my function for IsNull

function IsNull(const AValue: Variant): Boolean;
begin
  Result := VarIsNull(AValue) or VarIsEmpty(AValue) or VarIsClear(AValue);
end;
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33518664
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33518689
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
 

Author Closing Comment

by:Bianca
ID: 33523465
Works perfectly!!!
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33524412
fwiw, you'll back on that ... :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now