procedure MakeDBGridColumnsAutoFixItsWidth(objDBGrid:TDBGrid);
var
cc:integer;
i,tmpLength:integer;
objDataSet:TDataSet;
aDgCLength:array of integer;
begin
cc:=objDbGrid.Columns.Count-1;
objDataSet:=objDbGrid.DataSource.DataSet;
setlength(aDgCLength,cc+1);
//取標題字段的長度
for i:=0 to cc do
begin
aDgCLength[i]:= length(objDbGrid.Columns[i].Title.Caption);
end;
objDataSet.First;
while not objDataSet.Eof do
begin
//取列中每個字段的長度
for i:=0 to cc do
begin
tmpLength:=length(Trim(objDataSet.Fields.FIElds[i].AsString));
if tmpLength>aDgCLength[i]
then aDgCLength[i]:=tmpLength;
end;
objDataSet.Next;
end;
for i:=0 to cc do
begin
objDbGrid.Columns[i].Width := aDgCLength[i] * 8 ;
end;
end;