请教如何将dbgrid中的字段输出到stringgrid中 (50分)

  • 主题发起人 主题发起人 lj.ah
  • 开始时间 开始时间
L

lj.ah

Unregistered / Unconfirmed
GUEST, unregistred user!
点击字段名,在stringgrid中(原先为空)生成一列字段数据。
再点击另一字段名,又输出这列字段。
如何实现?
 
类似与导出到Excel中,逐个导入。
 
procedure TForm1.toGrid(Col: integer);
var
i:integer;
begin
stringgrid1.cells[col,0]:=DBGrid1.Columns.Items[col-1].FieldName;
with table1 do begin
first;
for i:=1 to RecordCount do begin
stringgrid1.cells[col,i]:=table1.fields[col-1].Asstring;
next;
end;
end;
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
i:byte;
begin
i:= Column.Index;
toGrid(i+1);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
table1.Open;
stringgrid1.ColCount:=table1.FieldCount+1;
stringgrid1.RowCount:=table1.RecordCount+1;
end;
 
变量toGrid在什么地方declare?
 
private
{ Private declarations }
procedure toGrid(Col: integer);
public
{ Public declarations }
end;
 
谢谢。
能否不输出字段名,而且输出列是紧挨着的,即顺序输出中间没有空列。
 
第一行就是字段名,如要按顺序排列,定义一个全局变量N,每增加一列,此全局变量增加一,
改:stringgrid1.cells[N,0]:=....
 
能不能说得详细些,最好能有源码
 
var
n:integer;

procedure TForm1.toGrid(Col: integer);
var
i:integer;
begin
n:=n+1;
stringgrid1.cells[n,0]:=DBGrid1.Columns.Items[col-1].FieldName;
with table1 do begin
first;
for i:=1 to RecordCount do begin
stringgrid1.cells[n,i]:=table1.fields[col-1].Asstring;
next;
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
n:=0;
table1.Open;
stringgrid1.ColCount:=table1.FieldCount+1;
stringgrid1.RowCount:=table1.RecordCount+1;
end;
 
其实我是想输出为一个矩阵的形式。所以stringgrid的行列只是选择的那几列,而不是
dbgrid的全部列。
 
还是有字段名啊,而且为什么一开始就从stringgrid的第二列输出哪,能不能从
第一列开始?
 
var
n:integer;

procedure TForm1.toGrid(Col: integer);
var
i:integer;
begin
n:=n+1;
stringgrid1.ColCount:=n+1;
stringgrid1.cells[n,0]:=DBGrid1.Columns.Items[col-1].FieldName;
with table1 do begin
first;
for i:=1 to RecordCount do begin
stringgrid1.cells[n,i]:=table1.fields[col-1].Asstring;
next;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
n:=0;
table1.Open;
stringgrid1.RowCount:=table1.RecordCount+1;
end;

 
更细的要求你可以自己动动手完成!
 
谢谢你!linsb
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部