procedure GridAutoWidth(objDBGrid:TwwDBGrid);
var
cc,x:integer;
i,tmpLength:integer;
objDataSet:TDataSet;
aDgCLength:array of integer;
begin
cc:=objDbGrid.FieldCount-1;
objDataSet:=objDbGrid.DataSource.DataSet;
setlength(aDgCLength,cc+1);
objDataSet.DisableControls;
try
//取标题字段的长度
for i:=0 to cc do
begin
aDgCLength[i]:= length(objDbGrid.Columns[i].DisplayLabel);
end;
objDataSet.First;
while not objDataSet.Eof do
begin
//取列中每个字段的长度
for i:=0 to cc do
begin
tmpLength:=length(objDataSet.Fields.Fields[i].AsString);
if tmpLength>aDgCLength[i] then
begin
if tmpLength > 36 then
aDgCLength[i] := 36
else
aDgCLength[i] := tmpLength;
end;
end;
objDataSet.Next;
end;
for i:=0 to cc do
begin
objDbGrid.Columns[i].DisplayWidth:=aDgCLength[i]+1;
if objDBGrid.Fields[i].DataType in [ftInteger,ftAutoInc] then
objDBGrid.Columns[i].DisplayWidth := 5;
end;
finally
objDataSet.EnableControls;
objDataSet.First;
end;
end;