参考 i:=0; adoquery.first; while not adoquery.eof do begin StringGrid1.RowCount:=i; for j:=0 to adoquery.fieldcount-1 do StringGrid1.cell[j,i]=adoquery.fields[j]; inc(i); adoquery.next end;
为什么不用DBGRid? Var sCol,I : Integer; //初始化StringGrid的抬头(Title) For sCol:=0 to StringGrid.ColCount-1 do begin StringGrid.Cells[sCol,0]:='Title'+IntToStr(I); end; I:=0; While not Query.EOF do begin StringGrid1.Cells[i,0]:=Query.FieldByName('学号').AsString; StringGrid1.Cells[i,1]:=Query.FieldByName('姓名').AsString; StringGrid1.Cells[i,2]:=Query.FieldByName('成绩').AsString; Inc(I); Query.Next; end;
procedure DataToStringGrid(dataset: TDataSet;dest: TStringgrid); var i,j: cardinal; blacklist: array of fieldinfo; begin if dataset.Active = true then begin // header setlength(blacklist,dataset.FieldCount); for i := 0 to dataset.FieldCount - 1 do begin if dataset.Fields[i].DataType in badtype then begin blacklist[i].cancast := false; //********************************// // do range check here blacklist[i].fType := FieldTypename[ord(dataset.Fields[i].DataType)]; end else begin blacklist[i].cancast := true; blacklist[i].fType := 'small fat pig'; end; dest.ColCount := dataset.FieldCount +1; dest.Cells[i+1,0] := dataset.Fields[i].FieldName; end; // body j := 0; dataset.First; while not dataset.Eof do begin inc(j); dest.Cells[0,j] := inttostr(j); for i := 0 to dataset.FieldCount -1 do begin if blacklist[i].cancast then begin dest.Cells[i+1,j] := dataset.Fields[i].AsString; end else begin dest.Cells[i+1,j] := blacklist[i].fType; end; end; dataset.Next; end; dest.RowCount := j + 1; end else begin cleangrid(dest,4,4); raise Exception.Create('dataset: ' + dataset.Name + ' not active'); end; end;
还有(从db.pas里改的),用的时候把活的tquery塞进去;cleangrid是自己写的reset grid用的 const badtype = [ftUnknown, {ftString, ftSmallint, ftInteger, ftWord, ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,} ftBytes, ftVarBytes, {ftAutoInc,} ftBlob, ftMemo, ftGraphic, ftFmtMemo, ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, {ftFixedChar, ftWideString, ftLargeint,} ftADT, ftArray, ftReference, ftDataSet, ftOraBlob, ftOraClob, ftVariant, ftInterface, ftIDispatch{, ftGuid, ftTimeStamp, ftFMTBcd}]; // FieldTypes commented are fields that can be casted into string // uncommented are bad type FieldTypeName:array [0..37] of string = ('Unknown', 'String', 'Smallint', 'Integer', 'Word', 'Boolean', 'Float', 'Currency', 'BCD', 'Date', 'Time', 'DateTime', 'Bytes', 'VarBytes', 'AutoInc', 'Blob', 'Memo', 'Graphic', 'FormattedMemo', 'Paradox OLE field', 'DBase OLE field', 'TypedBinary', 'cursor in Oracle stor_proc', 'FixedChar', 'WideString', 'Largeint', 'Abstract Data Type field', 'Array Field', 'Reference', 'DataSet Field', 'Oracle Blob', 'Oracle Clob', 'ftVariant', 'Interface', 'IDispatch', 'Guid', 'TimeStamp', 'FMTBcd'); procedure CleanGrid(grid: TStringGrid;default_row_count,default_col_count: byte); var i:integer; begin for i := grid.RowCount downto 0 do begin grid.Rows[i].Clear; end; grid.RowCount := default_row_count; grid.ColCount := default_col_count; end;
在DBGrid控件中显示图形 如果在数据库中设置了一个为BLOB类型的字段用于保存图形,在使用DBGrid控件显示时, 在表格中显示的是BLOB,而无法显示出图形,当然,有一些第三方控件可以显示出图形, 但是要去找第三方控件不是一件容易的事,而且有些好用的都需要付费。能不能在DBGrid 中显示图形呢?答案是肯定的。在DBGrid的OnDrawCell事件中加入如下代码即可在DBGrid 控件中显示图形。 var Bmp: TBitmap; begin if (Column.Field.DataTyp = ftBLOB) or (Column.Field.DataTyp = ftGraphic)then begin Bmp:=TBitmap.Create; try Bmp.Assign(Column.Field); DBGrid1.Canvas.StretchDraw(Rect,Bmp); Bmp.Free; Except Bmp.Free; end; end; end; 按照类似的方法,就可以在DBGrid中显示Memo类型的字段内容。 另外,在往数据库中保存图形时,建议使用EMF图元文件,这样数据库文件的大小不会变的 十分惊人,我试过了,同样是一幅400*300的图形,如果用位图,保存100多幅时,数据库文 件大小会达到近20MB,而使用EMF矢量图形保存,保存800多幅时才260多KB,保存EMF矢量图 形的方法与保存位图是差不多的,在DBGrid中显示也差不多,只不过BLOB型字段内容不能直 接Assign给EMF文件,要用MemoryStream来中转。
blacklist: array of fieldinfo fieldinfo是自己定义的关于field的结构,比如这个field能否cast成string或image
StringGrid1.Cell[col, Row] --- > 是字符类型, 接收当前的字符procedure TForm1.Button2Click(Sender: TObject);
var
myRect: TGridRect;
begin
StringGrid1.Cells[0,0] := 'hello'; // Notice this.
myRect.Left := StringGrid1.FixedCols; // first selected grid.
myRect.Top := StringGrid1.FixedRows;
myRect.Right := StringGrid1.ColCount - StringGrid1.FixedCols; // cols
myRect.Bottom := StringGrid1.RowCount - StringGrid1.FixedRows; // rows
StringGrid1.Selection := myRect;
end;
i:=0;
adoquery.first;
while not adoquery.eof do
begin
StringGrid1.RowCount:=i;
for j:=0 to adoquery.fieldcount-1 do
StringGrid1.cell[j,i]=adoquery.fields[j];
inc(i);
adoquery.next
end;
Var sCol,I : Integer;
//初始化StringGrid的抬头(Title)
For sCol:=0 to StringGrid.ColCount-1 do
begin
StringGrid.Cells[sCol,0]:='Title'+IntToStr(I);
end;
I:=0;
While not Query.EOF do
begin
StringGrid1.Cells[i,0]:=Query.FieldByName('学号').AsString;
StringGrid1.Cells[i,1]:=Query.FieldByName('姓名').AsString;
StringGrid1.Cells[i,2]:=Query.FieldByName('成绩').AsString;
Inc(I);
Query.Next;
end;
var
i,j: cardinal;
blacklist: array of fieldinfo;
begin
if dataset.Active = true then
begin
// header
setlength(blacklist,dataset.FieldCount);
for i := 0 to dataset.FieldCount - 1 do
begin
if dataset.Fields[i].DataType in badtype then
begin
blacklist[i].cancast := false;
//********************************//
// do range check here blacklist[i].fType := FieldTypename[ord(dataset.Fields[i].DataType)];
end
else begin
blacklist[i].cancast := true;
blacklist[i].fType := 'small fat pig';
end;
dest.ColCount := dataset.FieldCount +1;
dest.Cells[i+1,0] := dataset.Fields[i].FieldName;
end;
// body
j := 0;
dataset.First;
while not dataset.Eof do
begin
inc(j);
dest.Cells[0,j] := inttostr(j);
for i := 0 to dataset.FieldCount -1 do
begin
if blacklist[i].cancast then
begin
dest.Cells[i+1,j] := dataset.Fields[i].AsString;
end
else begin
dest.Cells[i+1,j] := blacklist[i].fType;
end;
end;
dataset.Next;
end;
dest.RowCount := j + 1;
end
else begin
cleangrid(dest,4,4);
raise Exception.Create('dataset: ' + dataset.Name + ' not active');
end;
end;
const
badtype =
[ftUnknown, {ftString, ftSmallint, ftInteger, ftWord, ftBoolean, ftFloat,
ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,} ftBytes, ftVarBytes, {ftAutoInc,}
ftBlob, ftMemo, ftGraphic, ftFmtMemo, ftParadoxOle, ftDBaseOle, ftTypedBinary,
ftCursor, {ftFixedChar, ftWideString, ftLargeint,} ftADT, ftArray, ftReference,
ftDataSet, ftOraBlob, ftOraClob, ftVariant, ftInterface, ftIDispatch{, ftGuid,
ftTimeStamp, ftFMTBcd}];
// FieldTypes commented are fields that can be casted into string
// uncommented are bad type FieldTypeName:array [0..37] of string =
('Unknown', 'String', 'Smallint', 'Integer', 'Word', 'Boolean', 'Float',
'Currency', 'BCD', 'Date', 'Time', 'DateTime', 'Bytes', 'VarBytes', 'AutoInc',
'Blob', 'Memo', 'Graphic', 'FormattedMemo', 'Paradox OLE field',
'DBase OLE field', 'TypedBinary', 'cursor in Oracle stor_proc',
'FixedChar', 'WideString', 'Largeint', 'Abstract Data Type field', 'Array Field',
'Reference', 'DataSet Field', 'Oracle Blob', 'Oracle Clob',
'ftVariant', 'Interface', 'IDispatch', 'Guid', 'TimeStamp', 'FMTBcd');
procedure CleanGrid(grid: TStringGrid;default_row_count,default_col_count: byte);
var i:integer;
begin
for i := grid.RowCount downto 0 do
begin
grid.Rows[i].Clear;
end; grid.RowCount := default_row_count;
grid.ColCount := default_col_count;
end;
如果在数据库中设置了一个为BLOB类型的字段用于保存图形,在使用DBGrid控件显示时,
在表格中显示的是BLOB,而无法显示出图形,当然,有一些第三方控件可以显示出图形,
但是要去找第三方控件不是一件容易的事,而且有些好用的都需要付费。能不能在DBGrid
中显示图形呢?答案是肯定的。在DBGrid的OnDrawCell事件中加入如下代码即可在DBGrid
控件中显示图形。
var Bmp: TBitmap;
begin
if (Column.Field.DataTyp = ftBLOB) or (Column.Field.DataTyp = ftGraphic)then
begin
Bmp:=TBitmap.Create;
try
Bmp.Assign(Column.Field);
DBGrid1.Canvas.StretchDraw(Rect,Bmp);
Bmp.Free;
Except
Bmp.Free;
end;
end;
end;
按照类似的方法,就可以在DBGrid中显示Memo类型的字段内容。
另外,在往数据库中保存图形时,建议使用EMF图元文件,这样数据库文件的大小不会变的
十分惊人,我试过了,同样是一幅400*300的图形,如果用位图,保存100多幅时,数据库文
件大小会达到近20MB,而使用EMF矢量图形保存,保存800多幅时才260多KB,保存EMF矢量图
形的方法与保存位图是差不多的,在DBGrid中显示也差不多,只不过BLOB型字段内容不能直
接Assign给EMF文件,要用MemoryStream来中转。
fieldinfo是自己定义的关于field的结构,比如这个field能否cast成string或image
写什么东西也是自己控制,
想写就写,不想写就不写,和你查询多少次一点关系也没有.
StringGrid1.Cell[col, Row] --- > 是字符类型, 接收当前的字符procedure TForm1.Button2Click(Sender: TObject);
var
myRect: TGridRect;
begin
StringGrid1.Cells[0,0] := 'hello'; // Notice this.
myRect.Left := StringGrid1.FixedCols; // first selected grid.
myRect.Top := StringGrid1.FixedRows;
myRect.Right := StringGrid1.ColCount - StringGrid1.FixedCols; // cols
myRect.Bottom := StringGrid1.RowCount - StringGrid1.FixedRows; // rows
StringGrid1.Selection := myRect;
end;