2012-10-15 75 views
9

它可能是基本的,但我有一個時間查找示例代碼的* *,用於根據數據庫中的值更改字符串網格的行顏色Firemonkey。我有一個來自MDB的數據沒有問題,但需要的行是一定的顏色,例如'1'=紅色'2'=綠色等。我知道我必須以某種方式訪問​​Style元素'OnApplyStyleLookup'?但在什麼階段。我看到了關於改變文字風格和顏色等方面的問題,但我正在挖掘一個洞,試圖進入「背景」元素並應用。任何幫助將不勝感激。 乾杯 理查德...(新手Firemonkey)根據來自數據的值在firemonkey stringgrid上着色單元格背景

+0

設置顏色你需要重寫的SetData方法該單元格,但單元類是在StringGrid中硬編碼的。您需要使用具有自定義單元類的普通網格來完成此操作。看看我的文章http://monkeystyler.com/blog/entry/firemonkey-grid-basics-custom-cells-and-columns,瞭解如何做到這一點。 –

+0

非常感謝Mike。你的文章看起來很好記錄。我全職工作(不是編程),所以我知道我將在這個週末做什麼。 –

回答

4
{OnDrawColumnCell event} 

procedure OnDrawColumnCell(Sender: TObject; 
    const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF; 
    const Row: Integer; const Value: TValue; const State: TGridDrawStates); 
var 
    RowColor : TBrush; 
begin 

    RowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha); 

{you can check for values and then set the color you want} 
    if Value.ToString = 'red' then 
    RowColor.Color := TAlphaColors.Red; 

    Canvas.FillRect(Bounds, 0, 0, [], 1, RowColor); 

    { perform default drawing } 
    TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row, 
    Value, State); 
end; 
0

這是我的代碼用Delphi柏林的正常工作:

var 
    aRowColor: TBrush; 
begin 
    //it's better to write this line into create 
    aRowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha); 
    //----- 
    grid.DefaultDrawing := False; 
    if (myTbl.RcrdDataCount > 0) and (Row < myTbl.RcrdDataCount) then begin 
    if myTbl.RcrdDataItems[Row].State = TStateDeleted then begin 
     aRowColor.Color := TAlphaColors.Red; 
    end 
    else begin 
     aRowColor.Color := TAlphaColors.Gray; 
    end; 
    Canvas.FillRect(Bounds, 0, 0, [], 1, aRowColor); 
    Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State); 
    end; 
    //it's better to write this line into destroy 
    aRowColor.free; 
    //----- 
end; 
相關問題