在再给您一点
--------------如果不行,权当灌水------------------
procedure Ttdeform.DoYss;
var debhtemp,gcmc:ansistring;
    desl:real;//定额数量
    gcbh:integer;//工程编号
    i,j:integer;
    xj:real;//小结合计
    hj:real;//合计
    rghj,//人工合计
    clhj,//材料合计
    jxhj:real;//机械合计
    rowcnt:integer;//计数器
    GcNum:integer;//工程计数器
begin
  with projectdatamodule.yssquery do //打开预算表
  begin
    Close;
    SQl.Clear;
    sql.Add('Select * from t_yss');
    prepared;
    open;
    first;
  end;
  with projectdatamodule.yssquery2 do//打开……
  begin
    Close;
    SQl.Clear;
    sql.Add('Select * from yss');
    prepared;
    open;
    if recordcount<>0 then //清除……
    begin
      for i:=1 to recordcount do delete;
      UpdateBatch;
    end;
  end;
  //复制数据从……
  with projectdatamodule do
  begin
    for j:=1 to yssquery.recordcount do
    begin
      yssquery2.Append;
      yssquery2.FieldByName('debh').AsString:=yssquery.FieldByName('debh').AsString;
      yssquery2.FieldByName('demc').AsString:=yssquery.FieldByName('demc').AsString;
      yssquery2.FieldByName('dw').AsString:=yssquery.FieldByName('dw').AsString;
      yssquery2.FieldByName('sl').Asfloat:=yssquery.FieldByName('sl').Asfloat;
      yssquery2.FieldByName('jj').Asfloat:=yssquery.FieldByName('jj').Asfloat;
      yssquery2.FieldByName('jjhj').Asfloat:=yssquery.FieldByName('jjhj').Asfloat;
      yssquery2.FieldByName('gcm').AsString:=yssquery.FieldByName('gcm').Asstring;
      yssquery2.Updatebatch;
      yssquery.Next;
    end;
    yssquery.first;
    yssquery2.first;
  end;
//清除stringgrid数据
  for i:=0 to stringgrid1.RowCount do
    for j:=0 to stringgrid1.ColCount do stringgrid1.Cells[j,i]:='';//第二步、分小结……
  with rationdatamodule.gcquery do
  begin
    close;
    sql.Clear;
    sql.Add('Select * from t_gcsm');
    prepared;
    open;
    first;
    rowcnt:=0;
    rghj:=0;
    clhj:=0;
    jxhj:=0;
    hj:=0;
    GcNum:=0;
    for i:=1 to recordcount do
    begin
      gcmc:=trim(fieldbyname('gcm').AsString);
      projectdatamodule.yssquery2.Close;
      projectdatamodule.yssquery2.SQl.Clear;
      projectdatamodule.yssquery2.sql.Add('Select * from yss');
      projectdatamodule.yssquery2.sql.Add('where gcm =:gcm');
      projectdatamodule.yssquery2.Parameters.ParamByName('gcm').value:=gcmc;
      projectdatamodule.yssquery2.prepared;
      projectdatamodule.yssquery2.open;
      projectdatamodule.yssquery2.first;
      if projectdatamodule.yssquery2.recordcount<>0 then//写数据到StringGrid
      begin
        GcNum:=GcNum+1;
        StringGrid1.Cells[0,rowcnt]:=NumExchange(GcNum);
        StringGrid1.Cells[1,rowcnt]:=gcmc;
        stringgrid1.RowCount:=stringgrid1.RowCount+1;
        rowcnt:=rowcnt+1;
        xj:=0;
        for j:=1 to projectdatamodule.yssquery2.recordcount do
        begin
          stringgrid1.Cells[0,rowcnt]:=inttostr(j);
          stringgrid1.Cells[1,rowcnt]:=projectdatamodule.yssquery2.fieldbyname('demc').AsString;
          stringgrid1.Cells[2,rowcnt]:=currtostr(projectdatamodule.yssquery2.fieldbyname('sl').Asfloat);
          stringgrid1.Cells[3,rowcnt]:=projectdatamodule.yssquery2.fieldbyname('dw').AsString;
          stringgrid1.Cells[4,rowcnt]:=format('%8.2f',[projectdatamodule.yssquery2.fieldbyname('jj').Asfloat]);
          stringgrid1.Cells[5,rowcnt]:=format('%8.2f',[projectdatamodule.yssquery2.fieldbyname('jjhj').Asfloat]);
          rghj:=rghj+projectdatamodule.yssquery2.fieldbyname('rghj').Asfloat;
          clhj:=clhj+projectdatamodule.yssquery2.fieldbyname('clhj').Asfloat;
          jxhj:=jxhj+projectdatamodule.yssquery2.fieldbyname('jxhj').Asfloat;
          xj:=xj+projectdatamodule.yssquery2.fieldbyname('jjhj').Asfloat;
          stringgrid1.RowCount:=stringgrid1.RowCount+1;
          rowcnt:=rowcnt+1;
          projectdatamodule.yssquery2.next;
        end;
        //小计
        stringgrid1.cells[1,rowcnt]:='小计';
        stringgrid1.cells[5,rowcnt]:=format('%8.2f',[xj]);
        hj:=hj+xj;
        stringgrid1.RowCount:=stringgrid1.RowCount+1;
        rowcnt:=rowcnt+1;
      end;
    next;
    end;
    stringgrid1.RowCount:=stringgrid1.RowCount+1;
    stringgrid1.cells[1,rowcnt]:='合计';
    stringgrid1.cells[5,rowcnt]:=format('%8.2f',[hj]);
  end;

解决方案 »

  1.   

    我的东东不多:
      生成query,将数据读入动态数组或tring;procedure Ttest.getinfo(astringgrid:Tstringgrid);
    var i:integer;
    begin
      cleargrid(astringgrid);  //清空stringgrid;不可少
      with Tquery.create(nil) do
      try
      databasename:='';
      close;
      sql.add('');
      prepare;
      open;last;first;        //
      if recordcount>0 then 
      astringgrid.rowcount:=recordcount+1;//多个标题栏
      for i:=1 to recordcount do  
      begin
        astringgrid.cells[0,i]:=fieldbyname('').asstring;
        。
        next;
      end;
      end;  
    end;
      
    如果你用stringgrid,也可以用动态数组将query中的数据读入,进行操作,最后结果再写入stringgrid中,不过注意别越界噢  :)