delphi gridpanel last row height issue

927 Views Asked by At

How to avoid gridpanel last row take higher than others? I have 10 rows with 10 percent value

enter image description here

If I append a new row with 0 percent the prior row which tend to be the last row take the right height but a new row borders appears at the bottom which I don't desire

enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

When SizeStyle=ssPercent the size calculation includes a double value (the percentage). In VCL the result must be rounded to a whole number (= pixels). The code uses Trunc(), presumably to assure that the number of rows fit into the GridPanel. Then, the excess pixels (if any) are "given" to the last row.

Instead of SizeStyle=ssPercent you can use SizeStyle=ssAbsolute and define the row heights as number of pixels. In this case the calculation does not include float values and there's no need for rounding. In this case you can declare the height of each row e.g. 28 an if the height of GridPanel1 is 280, then all rows are of equal height.

You can select SizeStyle in the IDE (Object Inspector) by selecting all TRowItem in the structure pane under RowCollection.

You can define these settings also at runtime.


Edit after comment

If you use SizeStyle=ssPercent, you need to make a choise between the two cases you show in your question.

If you use SizeStyle=ssAbsolute, you can use the TGridPanel1.OnResize() event to recalculate the item heights:

procedure TForm7.GridPanel1Resize(Sender: TObject);
var
  i, toth, rowh: integer;
  gp: TGridPanel;
begin
  gp := Sender as TGridPanel;

  toth := gp.Height;
  rowh := toth div gp.RowCollection.Count;

  for i := 0 to gp.RowCollection.Count-1 do
    gp.RowCollection.Items[i].Value := rowh;
end;

In addition, if the color of the GridPanel1 is the same as that of the form, you may want to set the GridPanel1.BevelOuter=bvNone which hides the border line of the GridPanel1 and the empty space that appears beneath the rows (when toth is not evenly divisable) becomes unnoticeable.