以下是程序具体的代码:
procedure TFrm_test.dbeh_1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
var
strColor :String;
BkColor :TColor;
begin
Try
strColor:=trim(qry_Renew.FieldByName('颜色值').AsString);
if(strColor='') then exit;
BkColor:=stringtocolor(strColor); //读取数据库表里的颜色值
(Sender as TDBGridEh).Canvas.Brush.Color:=BkColor;
with dbeh_1 do //显示数据的DBGridEh
begin
if (gdSelected in State) then //如果某行被选中
begin
Canvas.Brush.Color:= StringToColor('$00E7E6BE'); //背景色
Canvas.Font.Color:= clBlack; //字体色
end
else //其他没被选中的行按各自指定的颜色去显示
begin
Canvas.Brush.Color:=BkColor;
end;
DefaultDrawcolumnCell(rect,DataCol,Column,State);
end;
Except
On E:Exception Do
begin
MessageBox(Handle,PChar(E.Message),'失败...',MB_ICONSTOP) ;
exit;
end;
End;
end;
在我的这个例子里,我用test_main这个表保存记录的具体信息,在test_color表里保存记录的状态(这个是用户手动设定的,然后更新到一个下拉列表中去,在用户插入记录时可选择)和不同状态对应的颜色值。根据当前状态的不同去设置各行不同的颜色。
在qry_Renew里写如下的SQL语句:
Select A.Name,A.ID,A.Time,A.Status,B.颜色值 from test_main as A,test_color as B Where A.Status=B.Status
感谢程序人生给我提供的帮助,我现在将知识分享给大家,希望对大家有所帮助!
最后提供程序界面的截图:
see Yizero by yizero.com