A表                      B表
id  b_id  a_name      id   bume   
1    1     小张         1    管理部
2    3     小李         2    事业部
3    4     小王         3    销售部       
                      4    运营部在stringgrid 表里A和B合成显示成id    bume     a_name
1    管理部     小张
2    销售部     小李
3    运营部     小王我写的代码是这样的,请高手改一改或写你自己的代码
var i:Integer;
begin
with dm.Datato.Query1 do
begin
Close;
sql.Clear;
SQL.Add('select * from a,b where a.b_id=b.id');
Open;
end;
if dm.Datato.Query1.RecordCount>0 then
begin
i:=1;
StringGrid1.RowCount:=dm.Datato.Query1.RecordCount+1;
while not dm.Datato.Query1.eof do
begin
StringGrid1.Cells[0,1]:='1';
StringGrid1.Cells[0,i+1]:=IntToStr(i+1);
StringGrid1.Cells[1,i]:=dm.Datato.Query1.FieldByName('b.bume').Value;
StringGrid1.Cells[2,i]:=dm.Datato.Query1.Fields[2].Value;
StringGrid1.Cells[3,i]:=dm.Datato.Query1.Fields[3].Value;
inc(i);
Datato.Query1.Next;
end;

解决方案 »

  1.   

    将adoquery1中的值写入stringgrid
    procedure SetGridInfo(StrGrid:TStringGrid;sQuery:TADOQuery);
    var
      i,j:integer;
    begin
      StrGrid.RowCount:=2;
      i:=1;
      while not sQuery.eof do
      begin
        StrGrid.Cells[0,i]:=inttostr(i);
        for j:=1 to sQuery.FieldCount do
        begin
          StrGrid.Cells[j,i]:=sQuery.Fields[j-1].AsString;
        end;
        i:=i+1;
        StrGrid.RowCount:=StrGrid.RowCount+1;
        sQuery.Next;
      end;
    end;
      

  2.   

    'select * from a,b where a.b_id=b.id'改成只选择你需要的列
      

  3.   


    把sql查询语句改成这样就ok了
    with dm.Datato.Query1 do 
    begin 
      Close; 
      sql.Clear; 
      SQL.Add(select A.id, b,bume,a.a_name from a left join b on a.b_id = b.id'); 
      Open; 
    end;
      

  4.   


    能不能写完整代码呢,我开头也是这样弄.报错说是找不到b,bume .
      

  5.   

    结合你上面的代码.下面我的代码
    ..... 
    StringGrid1.Cells[0,1]:='1'; 
    StringGrid1.Cells[0,i+1]:=IntToStr(i+1); 
    StringGrid1.Cells[1,i]:=dm.Datato.Query1.FieldByName('b.bume').Value; 
    StringGrid1.Cells[2,i]:=dm.Datato.Query1.Fields[2].Value; 
    StringGrid1.Cells[3,i]:=dm.Datato.Query1.Fields[3].Value; 
    inc(i); 
    Datato.Query1.Next;
    .....报错提示:list index out of bonunds (3)
      

  6.   


    --查询
    with dm.Datato.Query1 do 
    begin 
      Close; 
      sql.Clear; 
      SQL.Add(select A.id, b.bume,a.a_name from a left join b on a.b_id = b.id'); 
      Open; 
    end;--写字段名
    for i:= 0 to dm.Datato.Query1.FieldCount-1 do
      StringGrid1.Cells[i,0]:= dm.Datato.Query1.Fields[i].FieldName;--写记录
    i:=1;
    while not dm.Datato.Query1.eof do 
    begin 
      for j:= 0 to dm.Datato.Query1.FieldCount-1 do
        StringGrid1.Cells[j,i]:= dm.Datato.Query1.Fields[j].AsString;
      inc(i); 
      dm.Datato.Query1.Next; 
    end;StringGrid1要先设置行数和列数
      

  7.   

    能不能写完整代码呢,我开头也是这样弄.报错说是找不到b,bume . 
    这是写错了,应该 b.bume   你需要自己看一下代码调试StringGrid1.Cells[0,1]:='1';
    StringGrid1.Cells[0,i+1]:=IntToStr(i+1);
    StringGrid1.Cells[1,i]:=dm.Datato.Query1.FieldByName('b.bume').Value;
    StringGrid1.Cells[2,i]:=dm.Datato.Query1.Fields[2].Value;
    StringGrid1.Cells[3,i]:=dm.Datato.Query1.Fields[3].Value;
    inc(i); 你这里的StringGrid1.Cells[0,i+1]是什么意思呀?
    试一下下面的
    StrGrid.RowCount:=2;
    i:=1;
    while not dm.Datato.Query1.eof do
    begin
      StringGrid1.Cells[0,i]:=IntToStr(i);
      StringGrid1.Cells[1,i]:=dm.Datato.Query1.FieldByName('b.bume').Value;
      StringGrid1.Cells[2,i]:=dm.Datato.Query1.Fields[2].Value;
      StringGrid1.Cells[3,i]:=dm.Datato.Query1.Fields[3].Value;  
      Inc(i);
      StrGrid.RowCount:=StrGrid.RowCount+1;
    end;