接Memo1的处理
在Adoquery表里有以下数据,
名称 中奖号码
特等奖 6A08
特等奖 203
四等奖 5009
四等奖 3109
四等奖 907
四等奖 16A03
四等奖 1305
四等奖 36A06
四等奖 308
四等奖 3906
四等奖 2507
四等奖 1311
三等奖 1005
三等奖 5509
三等奖 209
三等奖 3504
三等奖 3A02
一等奖 1805
现在要求在 Memo1 显示如下
特等奖
         6A08     203
四等奖    
         5009     3109
        907       16A03
       .......................
三等奖   
         1005      5509
        ............................

解决方案 »

  1.   


    var
        i:integer;
        strCurMC:string;
        strLine:string;
        iPos:integer;
    begin
        AdoQuery1.Close;
        AdoQuery1.Sql.Text:='select * from Table order by 名称';
        AdoQuery1.Open;    strCurMC:='';
        strLIne:='';
        iPos:=0;
        for i:=0 to AdoQuery1.RecordCount-1 do
        begin
            if strCurMC<>AdoQuery1.FieldByName('名称').AsString then
            begin
                strCurMC:=AdoQuery1.FieldByName('名称').AsString;
                Memo1.Lines.Add(strCurMC);
            end;
            strLIne:=strLIne+'      '+AdoQuery1.FieldByName('中奖号码').AsString;
            inc(iPos);
            if iPos=2 then
            begin
                Memo1.Lines.Add(strLine);
                strLIne:='';
                iPos:=0;
            end;        AdoQuery1.Next;
        end;
      

  2.   

    var
      I,J: Integer;
      str, strField, strValue: string;
    begin
      ...
      Adoquery.First;
      strField := '';
      str := '';
      J := 1;
      for i := 0 to Adoquery.RecordCount - 1 do
        with Adoquery do begin
          if strField <> FieldByName('名称').AsString then begin
            if str <> '' then
              Memo1.Lines.Add(str);
            strField := FieldByName('名称').AsString
            Memo1.Lines.Add(strField);
            str := '      ' + FieldByName('中奖号码').AsString;
            J := 1;
            Adoquery.Next;
            if Adoquery.Eof then begin
              Memo1.Lines.Add(str);
            end;
            continue;
          end;      str := str + '      ' + FieldByName('中奖号码').AsString;
          Inc(J);
          Adoquery.Next;      if (J = 2) or Adoquery.Eof then begin
            Memo1.Lines.Add(str);
            str := '';
            J := 1;
          end;
        end;
    end;
      

  3.   

    随手写的,也不知道行不行procedure TForm1.Button2Click(Sender: TObject);
    var
      Line, Level: string;
    begin
      ADOQuery1.DisableControls;
      Memo1.Lines.BeginUpdate;
      try
        ADOQuery1.Close;
        ADOQuery1.SQL.Text := 'select 名称 中奖号码 from 获奖人员表 order by 名称';
        ADOQuery1.Open;
        Memo1.Lines.Clear;
        if ADOQuery1.Eof then Exit;    Level := ADOQuery1.Fields[0].AsString;
        Memo1.Lines.Add(Level);
        while not ADOQuery1.Eof do
        begin
          if not SameText(Level, ADOQuery1.Fields[0].AsString) then
          begin
            Level := ADOQuery1.Fields[0].AsString;
            Memo1.Lines.Add(Level);
          end;      Line := StringOfChar(' ', 6) + ADOQuery1.Fields[1].AsString;      ADOQuery1.Next;
          //检测没有记录或者已经不是上一次的得奖等级,那么就把Line加入Memo1中
          if ADOQuery1.Eof or not SameText(Level, ADOQuery1.Fields[0].AsString) then
          begin
            Memo1.Lines.Add(Line);
            Continue;
          end
          else
          begin
            //如果还是上一次得奖等级,还记录,就串连
            Line := Line + StringOfChar(' ', 6) + ADOQuery1.Fields[1].AsString;
            Memo1.Lines.Add(Line);
          end;      ADOQuery1.Next;
        end;
      finally
        ADOQuery1.EnableControls;
        Memo1.Lines.EndUpdate;
      end;
    end;
      

  4.   


    不应该没数据显示,是不是查询没有结果?    AdoQuery1.Close;
        AdoQuery1.Sql.Text:='select * from Table order by 名称';
        AdoQuery1.Open;    AdoQuery1.Last;///加这一句
        strCurMC:='';
        strLIne:='';
        iPos:=0;
      

  5.   


    var 
      I,J: Integer; 
      str, strField, strValue: string; 
    begin 
      ...
      Adoquery.First; 
      strField := ''; 
      str := ''; 
      J := 1; 
      for i := 0 to Adoquery.RecordCount - 1 do 
        with Adoquery do begin 
          if strField  <> FieldByName('名称').AsString then begin
            if str  <> '' then 
              Memo1.Lines.Add(str); 
            strField := FieldByName('名称').AsString;
            Memo1.Lines.Add(strField);
            str := '      ' + FieldByName('中奖号码').AsString; 
            J := 1;
            Adoquery.Next; 
            if Adoquery.Eof then begin 
              Memo1.Lines.Add(str);
            end; 
            continue; 
          end;       str := str + '      ' + FieldByName('中奖号码').AsString; 
          Inc(J);
          Adoquery.Next;      if (J = 2) or Adoquery.Eof then begin
            Memo1.Lines.Add(str);
            str := '';
            J := 0;
          end;    end; 
    end;
      

  6.   

    前面的代码里有几个错误,下面的代码调试通过。procedure TForm1.Button1Click(Sender: TObject);
    var
        i:integer;
        strCurMC:string;
        strLine:string;
        iPos:integer;
    begin
        AdoQuery1.Close;
        AdoQuery1.Sql.Text:='select * from Table1 order by 名称';
        AdoQuery1.Open;
        AdoQuery1.Last;
        AdoQuery1.First;    strCurMC:='';
        strLIne:='';
        iPos:=0;
        for i:=0 to AdoQuery1.RecordCount-1 do
        begin
            if strCurMC<>AdoQuery1.FieldByName('名称').AsString then
            begin
                if (iPos=1) and (Length(strLine)>0) then
                    Memo1.Lines.Add(strLine);
                strCurMC:=AdoQuery1.FieldByName('名称').AsString;
                Memo1.Lines.Add(strCurMC);
                strLIne:='';
                iPos:=0;
            end;
            strLIne:=strLIne+'      '+AdoQuery1.FieldByName('号码').AsString;
            inc(iPos);
            if iPos=2 then
            begin
                Memo1.Lines.Add(strLine);
                strLIne:='';
                iPos:=0;
            end;        AdoQuery1.Next;
        end;
        if (iPos=1) and (Length(strLine)>0) then
            Memo1.Lines.Add(strLine);
    end;