Link to home
Start Free TrialLog in
Avatar of khaled salem
khaled salemFlag for United States of America

asked on

delphi DBgrid coloring

Hi all:

i am writing the following code to on the event DBgirddrawcell,
its working perfect, I'd like to call this event procedure somewhere in the form after update procedure to change the color of rows immediately. i don't know how to pass a parameters to procedure .

procedure TCashier1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
 const
   clPaleGreen = TColor($CCFFCC);
   clPaleRed =   TColor($CCCCFF);
 begin

     If Column.Field.Dataset.FieldbyName('dueamount').ASFLOAT =0 THEN

        If (gdFocused in State)Then
         Begin
         DBGRID1.Canvas.Font.Color:=CLyellow;
         DBGRID1.canvas.brush.color := clBlack;
         End
         Else
         BEGIN
         DBGRID1.canvas.brush.color := clPaleGreen;
         DBGRID1.Canvas.Font.Color:=CLBLUE;
         END;

     If (Column.Field.Dataset.FieldbyName('Status').AsInteger =2) and (Column.Field.Dataset.FieldbyName('paid').ASFLOAT <>0) THEN

        If (gdFocused in State)
         Then DBGRID1.canvas.brush.color := clBlack
         Else
         BEGIN
         DBGRID1.canvas.brush.color := clPaleRed;
         DBGRID1.Canvas.Font.Color:=CLBLUE;
         END;
     DBGRID1.DefaultDrawColumnCell(rect,DataCol,Column,State)
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of khaled salem

ASKER

i am trying , but the color doesn't appear .
which color? As the grid could be not focused, refreshing it from external procedure you should see it in palegreen or palered
Do not call DefaultDrawColumnCell at all, and set DefaultDrawing := False.
Hi all :

More explain,  the DBgrid1 is connected to MyQuery1. when i make double click on one row its open new form (receipt from) to receive the money, in this new form i make update to the row that selected by the main form i would like to change the color of selected row in the main form according to the update. without call the select query again to keep the cursor on this row.
but you have definitely to requery your new data, as you're updating them by an external object
Set query to be writable, make edit on current record, change values and post.
For Ado...

...before open query...
    
...
    MyQuery1.CursorLocation:=clUseClient;
    MyQuery1.CursorType:=ctStatic;
    MyQuery1.LockType:=ltPessimistic;
    MyQuery1.Open;
...

Open in new window

..on other form close...
...
  MyQuery1.FieldByName('Field1').ReadOnly := False;
  MyQuery1.Edit;
  MyQuery1.FieldByName('Field1').AsString := '123';
  MyQuery1.Post;
...

Open in new window


(...but this is different question :-)