Question

Given that I want my string grid to have equal height rows ...

I know that it will start with exactly 5 rows and I make it so at design time.

At run time I may want to add some extra rows (and I have code to not add below the bottom of the parent; at that point I rely on scroll bars).

It seems that sg.height := sg.height + sg.RowHeights[0]; is not producing the expected result - there is unexpecetd whitespace after the last row (I am still trying to calculate how the extra height corresponds to the number of rows, but it looks like it is the height of one row, no matter how many rows are in the grid).

Is there something else I should take into account? (perhaps GridLineWidth?) Or could I have set a property wrongly in the Object Inspector? Should I be using GridHeight, rather than Height?

Note: this problem only occurs when the stringgid is as wide as all cells and the scroll bar appears, drawing over part of the last column. If I leave an ugly blank sapce at the right of the string grid for the scroll bar to fit into, the problem does not occur.

Can anyone tell me the formula to calculate the new gridheight after adding a number of rows? Or wthere I am doing something wrong.


[Update] The fudge factor of 3 apears to correspond to the margin property

Was it helpful?

Solution

I will add my trial and error to your research in case it's of any value.
TMS AdvStringGrid. Here is an image of a TMS AdvStringGrid. I think it is largely based on an ordinary string grid with a lot of useful extensions. To make it easier to count rows, I magnified the image using SnagIt and made the pixel gridlines visible. I added some red lines to the top row of the grid. On this grid, the DefaultRowHeight is set to 21. Between the red lines, there are 5 pixels each so to get 21 pixels for the DefaultRowHeight you would have to include the stringgrid gridline which has a height of 1 pixel. When I load this grid, I use this code:

with gridLimits do
    begin
      RowCount := 3;
      Cells[0, 0] := 'Maximum Amount';
      Cells[0, 1] := 'Maximum Base';
      Cells[0, 2] := 'Maximum Period';
      IntegralHeight := False;
      MyHeight := 0;
      for i := 0 to RowCount - 1 do
        inc(MyHeight, RowHeights[i]);
      Height := MyHeight + 5;
    end;
end;

I captured the grid using SnagIt and let it capture an object on the screen. It included the 1 pixel grey line around the grid. The grid actually seems to visibly start at the blue line, so to get the height, I would think I would need to add the DefaultRowHeight times three plus the top blue line plus the bottom blue line, but if I do that and click on the bottom row, the grid scrolls up. It also scrolls up if I add 3 to the height. If I add 4 to the height, it does not scroll. I don't know why I set it on 5 and I will set it to 4 now because you can see that there is 1 pixel too much at the bottom.

It may be that the top and bottom grey lines should be added to get the total height of the grid and that is why it is the DefaultRowHeight times 3 plus the top and bottom lines plus two more for the grey lines that SnagIt included.

OTHER TIPS

Just in case anyone other than me is interested, here's what I did to get it to work.

When ading a row :

newHeight := stringGrid.Height + 
             stringGrid.RowHeights[Pred(stringGrid.Row)] + 
             stringGrid.GridLineWidth;  

if newHeight < maxheight then
   stringGrid.Height := newHeight;

stringGrid.RowCount := stringGrid.RowCount + 1;

Alternatively, I can leave it until I have added all rows and then

stringGrid.Height := ((stringGrid.RowHeights[1] + stringGrid.GridLineWidth) *              
                      (stringGrid.VisibleRowCount + stringGrid.FixedRows)) + 3;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top