3

I'm trying to replace True/False with Yes/No in a DBGrid. The code below almost works:

procedure TDatamodule1.DBGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);

  var
    sText : String;

begin
  if (UpperCase(Column.Field.FieldName) = UpperCase('Approved')) or
     (UpperCase(Column.Field.FieldName) = UpperCase('Obsolete')) then
    begin
      if Column.Field.Value = True then
        sText := 'Yes'
      Else
        If Column.Field.Value = False Then
          sText := 'No'
        Else
          sText := '';
      (Sender as TDBGrid).Canvas.FillRect(Rect);
      (Sender as TDBGrid).Canvas.TextRect(Rect, Rect.Left+3, Rect.Top+2, sText);
    end;
end;

This appeared to work until I started using the keyboard cursor keys to move around the grid. The cell that has the focus always has both True and Yes or False and No drawn on top of each other. How do I prevent the default True/False label from being drawn on the cell that has focus? All other cells of the grid are perfect.

Edit: I failed to mention that the grid is set to Readonly and I've noticed the problem is with both selected and focused. Thanks in advance.

2
  • Use the debugger to discover the source of the problem. Is the unwanted text already drawn when you enter this event handler? If so, then you need to do a better job covering it up. If not, then you need to find where it gets drawn, and then determine what condition you need to effect to suppress that later painting. As for your focus and selection problems, heed the State parameter. Sep 10, 2013 at 4:59
  • Thanks Rob. It took me quite a while but as you can see from the answer I posted. I did eventually figure it out. I also want to thank everyone for the other answers below. They are much more elegant than redrawing; however, I have bigger plans for this technique and must accept my own solution. My main question was how do I prevent the DBGrid from drawing over my changes. Again, thanks to all.
    – Mike
    Sep 11, 2013 at 18:00

4 Answers 4

11

A simple solution would be to set your Boolean field DisplayValues:

MyField.DisplayValues := 'Yes;No';
4

One way to achieve this is to set an OnGetText event handler for these fields. If you have static fields you can set those during design time. With dynamic fields you can do so in the FormCreate event.

procedure TMyForm.MyFieldGetText(Sender: TField; var Text: string; DisplayText: Boolean);
begin
  if Sender.AsBoolean then
    Text := 'Yes'
  else
    Text := 'No';
end;
1
  • Uwe, I'm not familiar enough with Delphi yet to understand how to apply this to "dynamic" fields. In this instance my clientdataset has no structure until the database query is executed (I have several queries). A procedure MyFieldGetText confuses me when "MyField" isn't known until after the query. I have to say I'm really impressed with the depth of knowledge here on SO, it's quite humbling to the beginner!
    – Mike
    Sep 11, 2013 at 18:32
0

I would say that you're working at the wrong level of abstraction. Use two calculated columns (AFlag and OFlag in the example below) and define an OnCalcFields event for your clientdataset. The below code is attached to a dataset called 'qCpap'.

procedure TView.qCpapCalcFields(DataSet: TDataSet);
begin
 if qCpapApproved.AsBoolean 
  then qCpapAFlag.AsString:= 'Yes'
  else qCpapAFlag.AsString:= 'No';

 if qCpapObsolete.AsBoolean 
  then qCpapOFlag.AsString:= 'Yes'
  else qCpapOFlag.AsString:= 'No';
end;

Incidentally, there's no point in using the 'uppercase' function on a string constant: you may as well write the string in uppercase yourself.

2
  • No'am, thanks for the reply. I'd prefer to redraw rather than have calc fields as I may use this concept for several other purposes. If redrawing can't be done, I'll try your suggestion. Noted on the uppercase comment, it is there for my own laziness. If I ever edit it later, I don't have to worry about using the wrong case.
    – Mike
    Sep 10, 2013 at 3:30
  • @Mike: try looking at the OnGetData event of the fields. I've never used this but I think that you can overload it and substitute your values here. It would be much simpler than your method. Sep 10, 2013 at 6:39
0

I resolved my issue with a lot of trial and error. First, in the DBGrid components I set dgEditing to false as it was unnecessary (my grid is readonly). This prevents the user from getting the cell into the focused mode. Second, I set DefaultDrawing to False. I updated my DBGridDrawColumnCell procedure as follows:

procedure TDatamodule1.DBGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);

  var
    sText : String;

begin
  if gdselected in state then { set proper colors if cell is selected }
    Begin
      (Sender as TDBGrid).Canvas.Font.Color := 
        TStyleManager.ActiveStyle.GetSystemColor(clwindowtext);
      (Sender as TDBGrid).Canvas.Brush.Color := 
        TStyleManager.ActiveStyle.GetSystemColor(clhighlight);
    End;

  if (UpperCase(Column.Field.FieldName) = 'APPROVED') or
     (UpperCase(Column.Field.FieldName) = 'OBSOLETE')) then
    begin
      if Column.Field.Value = True then
        sText := 'Yes'
      Else
        If Column.Field.Value = False Then
          sText := 'No'
        Else
          sText := '';
      (Sender as TDBGrid).Canvas.FillRect(Rect);
      (Sender as TDBGrid).Canvas.TextRect(Rect, Rect.Left+3, Rect.Top+2, sText);
    end
  Else
    Begin { I added this to draw all other columns as defaultdrawing is off }
      (Sender as TDBGrid).defaultdrawcolumncell(Rect, DataCol, Column, State);
    End;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.