贴一段TStringGrid的代码看看吧
========================================
function GetFieldSize(AField: TDataField): Integer;
begin
case AField.DataType of
TFieldType.ftBoolean:
Result := 20;
TFieldType.ftDate, TFieldType.ftDateTime, TFieldType.ftTime:
Result := 20;
TFieldType.ftInteger, TFieldType.ftLargeint, TFieldType.ftSmallint:
Result := 20;
TFieldType.ftFloat:
Result := 20;
else
Result := AField.Size;
end;
if Result<Length(AField.FieldName) then Result := Length(AField.FieldName);
end;
var
i, j: Integer;
begin
sgrdResult.ColCount := ADataSet.FieldCount+1;
sgrdResult.RowCount := ADataSet.RecordCount+1;
sgrdResult.ColWidths[0] := sgrdResult.Font.Size * 2;
for i:=0 to ADataSet.FieldCount-1 do
begin
sgrdResult.Cells[i+1, 0] := ADataSet.Fields.FieldName;
sgrdResult.ColWidths[i+1] := sgrdResult.Font.Size * GetFieldSize(ADataSet.Fields) ;
end;
j:=0;
ADataSet.First;
while not ADataSet.Eof do
begin
Inc(j);
sgrdResult.Cells[0, j] := SysUtils.IntToStr(j);
for i:=0 to ADataSet.FieldCount-1 do
sgrdResult.Cells[i+1, j] := ADataSet.Fields.AsString;
ADataSet.Next;
end;
end; |