5

I have DBGrid which stores client information and the expiry dates of memberships. I am using the following code on the OnDrawColumnCell event of the DBGrid to color rows which include memberships which are expiring (teal) or expired (red):

procedure TfrmMain.grdMainDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (ADOMember.FieldByName('expirydate').AsDateTime >= (now)) and (ADOMember.FieldByName('expirydate').AsDateTime <= (now+7)) then
  begin
    grdMain.Canvas.Brush.Color := clTeal;
    grdMain.Canvas.Font.Color := clWhite;
    grdMain.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
  if (ADOMember.FieldByName('expirydate').AsDateTime < (now)) and (ADOMember.FieldByName('expirydate').AsString <> '') then
  begin
    grdMain.Canvas.Brush.Color := clRed;
    grdMain.Canvas.Font.Color := clWhite;
    grdMain.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
end; 

I also have a button on my form which resets the DBGrid. This allows a user to show the full list of clients after a search has been completed (returning a small number of clients).

My problem occurs when pressing the reset button. The button executes the following SQL function (within a procedure called ResetMemberGrid) correctly, as the full list is displayed.

SELECT * FROM customers

However, the DBGrid is not colored anymore. All the rows remain white. I have not been able to call the grdMainDrawColumnCell procedure, as it requires parameters which I am not aware of. Is there a way to call the DrawColumnCell procedure? I have tried to Repaint, Invalidate and Refresh the DBGrid, with no luck. Thanks.

Arioch 'The
  • 15,799
  • 35
  • 62
Turtle254
  • 115
  • 3
  • 9
  • I've never seen the problem you're having, despite using the `DBGrid.OnDrawColumnCell` in many applications that are in daily use. One thing I do see, though, is that you don't need the two separate calls to `DefaultDrawColumnCell`; you should have a single one, just before the method's `end;`. The drawing code should be called in every case; the arguments passed to it will vary based on your `Brush` and `Font` colors, but it should *always* be called. – Ken White Jan 18 '14 at 15:58
  • @KenWhite I see. I've altered my code so that the DefaultDrawColumnCell is always called. Is there any way to call the DrawColumnCell procedure when needed? – Turtle254 Jan 18 '14 at 16:06
  • Pal, If you need to ask Mary what is her birthday, you would not ask each girl on the party for her name until found Mary, ask her, then forget it, go away then go next round for all the girls for their names... Three times in a row. `var f: TField; fd: TDate; ... F := ADOMember.FieldByName('expirydate'); Fd := f.AsDateTime;.... If Fd ≠ now then... If f.IsNull then... ` you do not need AsString at all – Arioch 'The Jan 18 '14 at 16:35
  • 1
    It should be fired by methods you've tried (`Repaint`, `Invalidate` or `Refresh`). But generally, you don't need to do anything. The `OnDrawColumnCell` event should be fired whenever the underlying data source changes or whenever the system asks the control for repaint. – TLama Jan 18 '14 at 16:35
  • show the codes that apply filtering and that undo filtering. your "executes the following SQL function" looks rather weird. Where and how does it execute it and why should the grid even pay any attention at the "function" executed elsewhere ? Do you just detach your grid from ADOMembers ? – Arioch 'The Jan 18 '14 at 17:31
  • what is written in `ResetMemberGrid` procedure – Anto Raja Prakash Jan 28 '14 at 12:19

0 Answers0