dbgrid中如何调整列宽 ( 积分: 100 )

  • 主题发起人 主题发起人 杨天林
  • 开始时间 开始时间

杨天林

Unregistered / Unconfirmed
GUEST, unregistred user!
dbgrid中,显示出来的数据总是太宽,如何调整?谢谢!
 
dbgrid中,显示出来的数据总是太宽,如何调整?谢谢!
 
(Sender as TDBGrid).Columns.Items[Index].Width
 
或者修改相应字段的DisplayWidth,呵呵
 
给你一函数:
//自动调整dbgrid行列
Procedure Tfmtwo.FixDBGridColumnsWidth(const DBGrid: TDBGrid);
var
i:integer;
totwidth:integer;
//定义整个宽度
VarWidth:integer;//定义变化的宽度
ResizableColumnCount:integer;//定义变化宽度列的总数
acolumn:Tcolumn;
begin
//在重新调整前所有列的宽度
TotWidth := 0;
VarWidth := 0;
//有多少列需要自动调整
ResizableColumnCount := 0;
for i := 0 to -1 + DBGrid.Columns.Count do
begin
TotWidth := TotWidth + DBGrid.Columns.Width;
if DBGrid.Columns.Field.Tag <> 0 then
Inc(ResizableColumnCount);
end;
//为每个列分隔线增加1PX
if dgColLines in DBGrid.Options then
TotWidth := TotWidth + DBGrid.Columns.Count;
if dgIndicator in DBGrid.Options then
TotWidth := TotWidth + IndicatorWidth;
VarWidth := DBGrid.ClientWidth - TotWidth;
//平均分配变化宽度的值
//给所有需要自动调整的列
if ResizableColumnCount > 0 then
VarWidth := varWidth div ResizableColumnCount;
for i := 0 to -1 + DBGrid.Columns.Count do
begin
AColumn := DBGrid.Columns;
if AColumn.Field.Tag <> 0 then
begin
AColumn.Width := AColumn.Width + VarWidth;
if AColumn.Width < AColumn.Field.Tag then
AColumn.Width := AColumn.Field.Tag;
end;
end;
end;
 
Column.Width:=Length(Column.Field.asstring)
 
列宽是调整了,但动态SQL提取数据,显示不出来,怎办?
 
dbgrid1.columns.items[0].fieldname:='wlh'; --字段名
dbgrid1.columns[0].title.caption:='物料号';
dbgrid1.columns[0].width:=150;
 
前台:
procedure load_DBgrideh_col(sender: tobject; id: integer);
var s: string;
i: integer;
begin

dm.sq := 'select * from zsp_autocol where FunID=' + inttostr(id);
dm.Open_SQ;
if dm.Q1.Eof then
begin
dm.sq := ' insert into zsp_autocol (FunID,comment) values (' + inttostr(id) + ',' + #39 + '铁路' + #39 + ')';
dm.Exe_SQ;
end;
dm.sq := 'select * from zsp_autocol where FunID=' + inttostr(id);
dm.Open_SQ;

if sender is tdbgrid then
begin
for I := 0 to (sender as tdbgrid).Columns.Count - 1 do
begin
(sender as tdbgrid).Columns.Items.Width := dm.gfI('COL' + INTTOSTR(I));
(sender as tdbgrid).Columns.Items.Title.Alignment := taCenter;
end;
end;

if sender is tdbgrideh then
begin
for I := 0 to (sender as tdbgrideh).Columns.Count - 1 do
begin
(sender as tdbgrideh).Columns.Items.Width := dm.gfI('COL' + INTTOSTR(I));
(sender as tdbgrideh).Columns.Items.Title.Alignment := taCenter;
end;
end;

end;

procedure save_DBgrideh_col(sender: tobject; id: integer);
var s: string;
i: integer;
begin
if dm.bCanConfigCol then
begin
s := 'update ZSP_AUTOCOL set comment=comment';
if sender is tdbgrideh then for I := 0 to (sender as tDBGridEh).Columns.Count - 1 do s := s + ',COL' + INTTOSTR(I) + '=' + inttostr((sender as tDBGridEh).Columns.Items.Width);
if sender is tdbgrid then for I := 0 to (sender as tDBGrid).Columns.Count - 1 do s := s + ',COL' + INTTOSTR(I) + '=' + inttostr((sender as tDBGrid).Columns.Items.Width);
s := s + ' where funid=' + inttostr(id);
dm.ExecSQL(s);
end;
end;
--------------------------------
后台:
create procedure zsp_查询GRIDE_COL
(
@FUNID INT,
@COMMENT NVARCHAR(100)
)
AS
if not exists(select * from zsp_autocol where FunID=@funID)
insert into zsp_autocol (FunID,comment) values (@funid,@comment)
select * from zsp_autocol where FunID=@funID

GO
----------
兄弟:可以调节 dbgrid/dbgrideh!
 
dbgrid1.columns[0].width:='宽度';
 
后退
顶部