请教各位高手,这种格式的数据表在stringgrid中显示该如何写代码(80分)

  • 主题发起人 主题发起人 jygljf11111
  • 开始时间 开始时间
J

jygljf11111

Unregistered / Unconfirmed
GUEST, unregistred user!
从一个已有的数据库中读一个表,<br>表结构如下:<br>字段1 &nbsp; &nbsp;字段2 <br>a &nbsp; &nbsp; &nbsp; &nbsp; 10<br>b &nbsp; &nbsp; &nbsp; &nbsp; 25<br>c &nbsp; &nbsp; &nbsp; &nbsp; 18 &nbsp;<br>d &nbsp; &nbsp; &nbsp; &nbsp; 21<br>在stringgrid中显示为:<br>a &nbsp; &nbsp;b &nbsp; &nbsp;c &nbsp; &nbsp; d<br>10 &nbsp; 25 &nbsp; 18 &nbsp; &nbsp;21<br>请各位高手给予指点,谢谢!!!.
 
为什么没有人回答啊,难道没有人会吗?
 
看来要自已写自循环了:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;ID: integer;<br>begin<br> &nbsp;ADOQuery1.Close;<br> &nbsp;ADOQuery1.SQL.Text:= 'select a=字段1, b=字段2 from 表名';<br> &nbsp;ADOQuery1.Open;<br> &nbsp;StringGrid1.RowCount:= 2;<br> &nbsp;StringGrid1.ColCount:= ADOQuery1.RecordCount;<br> &nbsp;StringGrid1.FixedCols:= 0;<br> &nbsp;StringGrid1.FixedRows:= 1;<br> &nbsp;ID:= 0;<br> &nbsp;ADOQuery1.First;<br> &nbsp;while not ADOQuery1.Eof do<br> &nbsp;begin<br> &nbsp; &nbsp;StringGrid1.Cells[ID,0]:= ADOQuery1.FieldByName('a').AsString;<br> &nbsp; &nbsp;StringGrid1.Cells[ID,1]:= ADOQuery1.FieldByName('b').AsString;<br> &nbsp; &nbsp;ID:= ID + 1;<br> &nbsp; &nbsp;ADOQuery1.Next;<br> &nbsp;end;<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;iCount,i,j:Integer;<br>begin<br> &nbsp;j:=0;<br> &nbsp;with ADOQuery1 do<br> &nbsp;try<br> &nbsp; &nbsp;Close;<br> &nbsp; &nbsp;SQL.Text:='select * from CatalogM';<br> &nbsp; &nbsp;Open;<br> &nbsp; &nbsp;if RecordCount=0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;iCount:=FieldCount-1;<br> &nbsp; &nbsp;StringGrid1.ColCount:=RecordCount; &nbsp; //这里列的长度应该根据记录变化<br> &nbsp; &nbsp;StringGrid1.RowCount:=Fields.Count; &nbsp;//这里行数应该根据字段变化<br> &nbsp; &nbsp;while not eof do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;for i:=0 to iCount do<br> &nbsp; &nbsp; &nbsp;StringGrid1.Cells[j,i]:=Fields.AsString;<br> &nbsp; &nbsp; &nbsp;Inc(j);<br> &nbsp; &nbsp; &nbsp;Next;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Close;<br> &nbsp;except<br> &nbsp;end;<br>end;
 
后退
顶部