DBGridEH根據某一欄位不同值顯示不同的行顏色

來源:互聯網
上載者:User

以下是程式具體的代碼:

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.