在drawcolumncell事件裡面處理:procedure TDelyList_frm.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); const CtrlState :array[Boolean] of Integer=(DFCS_BUTTONCHECK,DFCS_BUTTONCHECK or DFCS_CHECKED); begin if Column.Field.DataType =ftBoolean then begin DBGrid1.Canvas.FillRect(Rect); DrawFrameControl(DBGrid1.Canvas.Handle,Rect,DFC_BUTTON,CtrlState[Column.Field.AsBoolean]); end; end;
在DBGrid的DrawColumnCell事件中procedure TfrmBetterCust.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var str:string; begin if Column.FieldName='是否' then //字段为"是否" begin str:=Column.Field.AsString ; if str='0' then begin DBGrid1.Canvas.Font.Color:=clRed; DBGrid1.Canvas.TextOut(Rect.Left+1,Rect.Top+2,'是'); end else if str='1' then begin DBGrid1.Canvas.Font.Color:=clBlue; DBGrid1.Canvas.TextOut(Rect.Left+1,Rect.Top+2,'否'); end; end else DBGrid1.DefaultDrawDataCell(Rect,Column.Field,state); end;OK!搞定了!
要这么麻烦吗? 在数据集(如:TADOQUERY)中设置永久字段不就搞定了吗,
呵呵,楼上各位都太夸张了,我来个更夸长的select sex , case sex when true then '男' when false then '女' when null then '人妖' end as 性别 from employee
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
const
CtrlState :array[Boolean] of Integer=(DFCS_BUTTONCHECK,DFCS_BUTTONCHECK or DFCS_CHECKED);
begin
if Column.Field.DataType =ftBoolean then
begin
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle,Rect,DFC_BUTTON,CtrlState[Column.Field.AsBoolean]);
end;
end;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var
str:string;
begin
if Column.FieldName='是否' then //字段为"是否"
begin
str:=Column.Field.AsString ;
if str='0' then
begin
DBGrid1.Canvas.Font.Color:=clRed;
DBGrid1.Canvas.TextOut(Rect.Left+1,Rect.Top+2,'是');
end
else if str='1' then
begin
DBGrid1.Canvas.Font.Color:=clBlue;
DBGrid1.Canvas.TextOut(Rect.Left+1,Rect.Top+2,'否');
end;
end
else
DBGrid1.DefaultDrawDataCell(Rect,Column.Field,state);
end;OK!搞定了!
在数据集(如:TADOQUERY)中设置永久字段不就搞定了吗,
case sex
when true then '男'
when false then '女'
when null then '人妖'
end as 性别
from employee
可以设置该布尔性字段的DisPlayValues设为:是;否
显示时就会显示是或否了
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
TBooleanField(adoquery1.fieldbyname('flag')).DisplayValues:='是;否';
//flag为字段名
end;
if(sender.AsInteger=0)then
Text:='男'
else
Text:='女';