何必那样呢 在数据集里用gettext和settext是最正宗的方法 双击你的数据集,然后单击右键,选择添加所有字段,然后选择你的这个颜色字段,在事件里找gettext和settext procedure TForm1.DataSetFieldNameGetText(Sender: TField; var Text: String; DisplayText: Boolean); begin if Sender.Value=1 then Text:='红' else if Sender.Value=1 then Text:='蓝' else if Sender.Value=1 then Text:='绿'; end; 至于settext你就把它反过来就行了
添加dbgrid的onDrawColumnCell事件
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (DataCol=0) then //此处根据你的列字段在dbgrid中的顺序来设置,如你color字段在第而列,则设置为1
begin
case adoquery1.FieldByName('color').Value of //获取你color字段的值,根据值进行单元格背景重绘
0:Dbgrid1.Canvas.Brush.color:=clred; //color字段值为0,背景色为红色
1:Dbgrid1.Canvas.Brush.color:=clblue;//color字段值为1,背景色为蓝色
2:Dbgrid1.Canvas.Brush.color:=cllime;//同上
3:Dbgrid1.Canvas.Brush.color:=clyellow;//同上
4:Dbgrid1.Canvas.Brush.color:=claqua;//同上
end;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);//系统默认
end;
end;
或许尝试建立一表(图片表)与此表对应,在DBGRID中显示相应的图片。
关于在DBGRID中显示图片的文章请另行搜索!
(根据需要的效果自己制作不同颜色)然后在dbgrid里就显示图片即可。关于在DBGRID中显示图片的文章请另行搜索!
在数据集里用gettext和settext是最正宗的方法
双击你的数据集,然后单击右键,选择添加所有字段,然后选择你的这个颜色字段,在事件里找gettext和settext
procedure TForm1.DataSetFieldNameGetText(Sender: TField; var Text: String;
DisplayText: Boolean);
begin
if Sender.Value=1 then Text:='红'
else if Sender.Value=1 then Text:='蓝'
else if Sender.Value=1 then Text:='绿';
end;
至于settext你就把它反过来就行了
1.可以使用TField对象的GetText事件对数据库中的值进行转换后再显示;
2.在数据集中添加一个计算字段,其值依据Color字段的值而定,在DBGrid中显示这个计算字段即可。