以下是程式具體的代碼:
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