设置 TDBGrid 可见行
Setting TDBGrid visible rows
我想根据 VisibleRows
参数调整 TDBGrid 高度。网格可能有也可能没有标题。
假设我 select 来自数据库的 100 条记录,但我希望调整网格高度以显示前 10 行(使它们可见)。数据集仍将保留 100 条记录。
即
procedure SetVisibleRows(DBGrid: TCustomDBGrid; VisibleRows: Integer);
begin
...
DBGrid.Height := ???
end;
我知道如何获取可见行:
type
TCustomGridHack = class(TCustomGrid);
function GetVisibleRows(DBGrid: TCustomDBGrid): Integer;
begin
Result := TCustomGridHack(DBGrid).VisibleRowCount;
end;
但是,有没有办法设置 VisibleRowCount
?
这似乎可行(也许可以进一步优化):
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;
我想根据 VisibleRows
参数调整 TDBGrid 高度。网格可能有也可能没有标题。
假设我 select 来自数据库的 100 条记录,但我希望调整网格高度以显示前 10 行(使它们可见)。数据集仍将保留 100 条记录。
即
procedure SetVisibleRows(DBGrid: TCustomDBGrid; VisibleRows: Integer);
begin
...
DBGrid.Height := ???
end;
我知道如何获取可见行:
type
TCustomGridHack = class(TCustomGrid);
function GetVisibleRows(DBGrid: TCustomDBGrid): Integer;
begin
Result := TCustomGridHack(DBGrid).VisibleRowCount;
end;
但是,有没有办法设置 VisibleRowCount
?
这似乎可行(也许可以进一步优化):
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;