我有个表
---------------------------
stuNo    stuName    sex
--------------------------
101      Jim         1
108      Tom         1
102      Sam         0
...
---------------------------
我如何利用OnDrawDataCell,让绘制这张表的时候,一旦遇到性别sex的值是1的时候,就把这个1变成红色字体,其他颜色不变,如何实现?

解决方案 »

  1.   


    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    begin
      if Field.FieldName <> 'sex' then Exit;
      if Field.AsInteger = 1 then
      begin
        DBGrid1.Canvas.Font.Color :=clRed;
        DBGrid1.Canvas.TextOut(Rect.Left,Rect.Top,Field.AsString);
      end;
    end;
      

  2.   


    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    begin
      if (field.Index = 2) and (table1.FieldByName('sex').Value = '1') then
        dbgrid1.Canvas.Font.Color:= clred;
      dbgrid1.DefaultDrawDataCell(Rect,Field,state);
    end;