2012-03-12 76 views
2

我正在運行Lazarus v0.9.30(32位編譯器)。爲什麼加載網格時TStringGrid列標題單元格中的單元格屬性發生更改?

此問題是我以前的question的延伸。

上一個問題圍繞着如何將運行時加載的TGridColumns對象中的文本方向更改爲標準TStringGrid。該解決方案涉及重寫字符串網格的DrawCellText事件。

我的問題是這樣的。當我嘗試加載TStringGrid時,我發現文本方向保持不變,但列單元格高度變回默認高度。

我用來加載網格的代碼如下所示。

procedure TTmMainForm.vLoadWorldScoutGrid; 
var 
    aMember : TTmMember; 
    anIndex1: integer; 
    anIndex2: integer; 
begin 
    //Clear the string grid and set the row count to 1 to take into account the fixed row. 
    SgWorldScout.Clear; 
    SgWorldScout.RowCount := 1; 

    for anIndex1 := 0 to Pred(FManager.Members.Count) do 
    begin 
    //Add a new row to the string grid. 
    SgMembers.RowCount := SgMembers.RowCount + 1; 

    //Get the TTmMember object from the collection. 
    aMember := TTmMember(FManager.Members.Items[anIndex1]); 

    //Populate the row cells in the string grid. 
    SgMembers.Cells[0, SgMembers.RowCount - 1] := aMember.stMemberNumber; 
    SgMembers.Cells[1, SgMembers.RowCount - 1] := aMember.stPatrol; 
    SgMembers.Cells[2, SgMembers.RowCount - 1] := aMember.stSurname; 
    SgMembers.Cells[3, SgMembers.RowCount - 1] := aMember.stFirstName; 

    //Add the TTmMember object to every row cell. 
    for anIndex2 := 0 to SgMembers.ColCount - 1 do 
     SgMembers.Objects[anIndex2, SgMembers.RowCount - 1] := aMember; 
    end; {for}} 

    vSetWorldScoutGridPushbuttons; 
end; 

我懷疑,當我打電話,字符串網格單元的性能可能會重置/修改默認DrawCellText事件被調用,這可以解釋在細胞高度的變化「SgWorldScout.Clear」。不知道爲什麼文本方向也不會改變。有人能夠解釋DrawCellText事件的行爲,爲什麼我會看到這一點?

+0

在此過程中究竟希望您清楚的是什麼?你真的想清除整個網格嗎?有一組函數,['TCustomStringGrid.Clean'](http://lazarus-ccr.sourceforge.net/docs/lcl/grids/tcustomstringgrid.clean.html)僅用於清理特定單元格的單元格內容電網的區域。 – TLama 2012-03-12 09:28:31

+0

是的我只想清理數據行...不是固定的行。所以我想要清除所有非固定單元格文本和任何指向關聯對象的指針。 – user1174918 2012-03-12 11:48:35

+1

然後回答如下:)你不需要清除整個網格。將行數設置爲所需值是非常安全的(並且常用),所以只需將「RowCount」設置爲1(不帶「Clear」)就可以保留標題(包含所有設置)。 – TLama 2012-03-12 11:49:32

回答

2

ClearRowCountColCount設置爲0,正如您懷疑的那樣。然後,RowHeights也會被清除,因爲當您將RowCount設置爲0時,沒有高度可以存儲。如果你想清除並只添加非固定行,那麼只需將RowCount設置爲1而不必清除整個網格。因此,請按照以下方式修改您的代碼:

procedure TTmMainForm.vLoadWorldScoutGrid; 
var 
    aMember : TTmMember; 
    anIndex1: integer; 
    anIndex2: integer; 
begin 
    // set the row count to 1 to keep the fixed row along with its settings 
    SgWorldScout.RowCount := 1; 

    for anIndex1 := 0 to Pred(FManager.Members.Count) do 
    ... 
end; 
相關問題