这似乎起作用了(也许可以进一步优化):
type
TCustomDBGridHack = class(TCustomDBGrid);
procedure SetVisibleRows(DBGrid: TCustomDBGrid; VisibleRows: Integer);
var
TitleHeight, RowHeight: Integer;
begin
with TCustomDBGridHack(DBGrid) do
begin
if dgTitles in Options then
begin
TitleHeight := RowHeights[0] + GridLineWidth;
RowHeight := RowHeights[1] + GridLineWidth;
end
else
begin
TitleHeight := 0;
RowHeight := RowHeights[0] + GridLineWidth;
end;
end;
DBGrid.ClientHeight := TitleHeight + (RowHeight * VisibleRows) + 1;
end;
或者按照 @nil 建议的方式,使用 TGridDrawInfo
。它可以产生更准确的结果(我稍作修改):
procedure SetVisibleRows(DBGrid: TCustomDBGrid; VisibleRows: Integer);
var
DrawInfo: TGridDrawInfo;
TitleHeight, RowHeight: Integer;
begin
with TCustomDBGridHack(DBGrid) do
begin
CalcDrawInfo(DrawInfo);
TitleHeight := DrawInfo.Vert.FixedBoundary;
RowHeight := RowHeights[DrawInfo.Vert.FirstGridCell] + DrawInfo.Vert.EffectiveLineWidth;
end;
DBGrid.ClientHeight := TitleHeight + (RowHeight * VisibleRows) + 1;
end;
正如@nil所提到的,
RowHeight
也可以通过以下方式进行计算:
RowHeight := DrawInfo.Vert.GetExtent(DrawInfo.Vert.FirstGridCell) + DrawInfo.Vert.EffectiveLineWidth;
我没有注意到任何不同之处(应进一步调查)。
上述内容可以进一步改进,使TDBGrid滚动条更好地调整:
procedure SetVisibleRows(DBGrid: TCustomDBGrid; VisibleRows: Integer);
var
DrawInfo: TGridDrawInfo;
TitleHeight, RowHeight: Integer;
HasActiveDataSet: Boolean;
begin
if VisibleRows < 0 then VisibleRows := 0;
HasActiveDataSet := Assigned(DBGrid.DataSource) and
Assigned(DBGrid.DataSource.DataSet) and
DBGrid.DataSource.DataSet.Active;
if HasActiveDataSet then
DBGrid.DataSource.DataSet.DisableControls;
try
with TCustomDBGridHack(DBGrid) do
begin
CalcDrawInfo(DrawInfo);
TitleHeight := DrawInfo.Vert.FixedBoundary;
RowHeight := RowHeights[DrawInfo.Vert.FirstGridCell] + DrawInfo.Vert.EffectiveLineWidth;
end;
DBGrid.ClientHeight := TitleHeight + (RowHeight * VisibleRows) + 1;
finally
if HasActiveDataSet then
DBGrid.DataSource.DataSet.EnableControls;
end;
end;
GetVisibleRowCount
方法,但是网格客户端高度可以通过TGridDrawInfo
进行设置吗?我只发现了对它的计算。如果你有可用的代码,请随意分享 :) - zigDBGrid.ClientHeight := AClientHeight; end;
- nil