由于要在网格中显示合计分数,我想用TStringGrid 显示数据库的内容。但在运行显示下列内容:FOR-Loop variable 'i' may be undefined after loop,请问该如何修改。
另外如何让网格中的内容靠右显示,标题居中显示,并且单独设好每一列的宽度。
代码如下:StringGrid1.ColCount := 5;
  StringGrid1.RowCount := Query.RecordCount + 2;
  StringGrid1.Cells[0,0] := '学号';
  StringGrid1.Cells[1,0] := '数学';
  StringGrid1.Cells[2,0] := '语文';
  StringGrid1.Cells[3,0] := '英语';
  StringGrid1.Cells[4,0] := '计算机';
  for i:=1 to Query.RecordCount  do
  begin
    StringGrid1.Cells[0,i] := Query.FieldByName('xh').AsString;
    StringGrid1.Cells[1,i] := Query.FieldByName('sx').AsString;
    StringGrid1.Cells[2,i] := Query.FieldByName('yw').AsString;
    StringGrid1.Cells[3,i] := Query.FieldByName('yy').AsString;
    StringGrid1.Cells[4,i] := Query.FieldByName('jsj').AsString;
    Query.Next;
  end;     
  StringGrid1.Cells[0,i] := '合计';
  //计算合计
  Query.First;
  a := 0;
  while not Query.Eof do
  begin
    a := a + StrToFloat(Query.FieldByName('xh').AsString);
    Query.Next;
  end;
  StringGrid1.Cells[1,i] := FloatToStrF(a,ffFixed,20,0);  

解决方案 »

  1.   

    StringGrid1.ColCount := 5;
      StringGrid1.RowCount := Query.RecordCount + 2;
      StringGrid1.Cells[0,0] := '学号';
      StringGrid1.Cells[1,0] := '数学';
      StringGrid1.Cells[2,0] := '语文';
      StringGrid1.Cells[3,0] := '英语';
      StringGrid1.Cells[4,0] := '计算机';
      for i:=1 to Query.RecordCount  do
      begin
        StringGrid1.Cells[0,i] := Query.FieldByName('xh').AsString;
        StringGrid1.Cells[1,i] := Query.FieldByName('sx').AsString;
        StringGrid1.Cells[2,i] := Query.FieldByName('yw').AsString;
        StringGrid1.Cells[3,i] := Query.FieldByName('yy').AsString;
        StringGrid1.Cells[4,i] := Query.FieldByName('jsj').AsString;
        Query.Next;
      end;     
      i:=Query.RecordCount+1;  
      StringGrid1.Cells[0,i] := '合计';
      //计算合计
      Query.First;
      a := 0;
      while not Query.Eof do
      begin
        a := a + StrToFloat(Query.FieldByName('xh').AsString);
        Query.Next;
      end;
      StringGrid1.Cells[1,i] := FloatToStrF(a,ffFixed,20,0);
      

  2.   

    //----------------------------------------------------
    //  date:2002-12-11
    //  auther: ycomputerunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids, cxControls, cxContainer, cxEdit, cxTextEdit,
      cxCurrencyEdit;type
        Tfrmmain = class(TForm)
        StringGrid1: TStringGrid;
        ComboBox1: TComboBox;
        Edit1: TcxCurrencyEdit;
        procedure FormCreate(Sender: TObject);
        procedure ComboBox1Change(Sender: TObject);
        procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
          var CanSelect: Boolean);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure Edit1Change(Sender: TObject);
        procedure Edit1PropertiesChange(Sender: TObject);
      private
        procedure CMDialogKey(var msg:TCMDialogKey);
        { Private declarations }
      public
        { Public declarations }
      end;var
      frmmain: Tfrmmain;implementation{$R *.dfm}
    //--------- message function  it show me the combobox and stringgrid Synchronous
     procedure Tfrmmain.CMDialogKey(var msg:TCMDialogkey);
     begin
       if (ActiveControl=combobox1) then
          begin
           if (msg.CharCode =VK_TAB) then
             begin
               //setfocus back to the grid and pass the tab key to it
               combobox1.SetFocus;
               combobox1.Perform(WM_KEYDOWN,msg.CharCode,msg.KeyData);
               //swallow this message
               msg.Result:=1;
               exit;         end;
          end;
         inherited;
     end;procedure Tfrmmain.FormCreate(Sender: TObject);
     var i:integer; begin
     stringgrid1.DefaultRowHeight:=combobox1.Height;
     combobox1.Visible:=false;
     stringgrid1.Cells[0,0]:='Row No';
     stringgrid1.Cells[1,0]:='Values (from combobox)';
     for i:=0 to stringgrid1.RowCount-1 do
         begin
          stringgrid1.Cells [0,i]:=inttostr(i);
         end;
    end;
    //-----set the text of the combobox to the stringgrid
    procedure Tfrmmain.ComboBox1Change(Sender: TObject);
    var introw:integer;
    begin
      inherited;
      with combobox1 do
          begin
           introw:=stringgrid1.Row;
           if (stringgrid1.col=2) then
               stringgrid1.Cells[1,introw]:=items[itemindex]
           else
           //  stringgrid1.Cells[stringgrid1.Col,introw]:=items[ItemIndex];
             visible:=false;
             end;
             stringgrid1.SetFocus;end;
     //- set the Position of combobox  as you  select on the stringgrid
    procedure Tfrmmain.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
      var R:TRect;
      begin
      if ((acol=1) and (arow<>0)) then
      // if  (arow<>0) then
        begin
          R:=stringgrid1.CellRect(acol,arow); //---left ,right,top bottom
          R.Left:=r.Left+stringgrid1.Left;
          R.Right:=r.Right+stringgrid1.Left;
          R.Top:=r.Top+stringgrid1.Top;
          R.Bottom:=r.Bottom+stringgrid1.Top;
         with combobox1 do
           begin                   //---width and height
            left:=r.Left+1;
            top:=r.Top +1;
            width:=(r.Right+1)-r.Left;
            height:=(r.Bottom+1)-r.Top;
            ItemIndex:=Items.IndexOf(stringgrid1.Cells[acol,arow]);
            visible:=true;
            setfocus;
            end;
          end;
         canselect:=true;
        if ((acol=2) and (arow<>0)) then
            begin
             r:=stringgrid1.CellRect(acol,arow);
             r.Left:=r.Left+stringgrid1.Left;
             r.Right:=r.Right+stringgrid1.Left;
             r.Top:=r.Top+stringgrid1.Top;
             r.Bottom:=r.Bottom+stringgrid1.Top;         with edit1 do
               begin
               left:=r.Left+1;
               top:=r.Top+1;
               width:=(r.Right+1)-r.Left;
               height:=(r.Bottom+1)-r.Top;
               visible:=true;
               
               setfocus;           end;
            end;
    end;
      //set the font of the stringgrid and so on
    procedure Tfrmmain.StringGrid1DrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
      const
      AlignFlags:array[TAlignment] of integer=(DT_LEFT or DT_VCENTER OR DT_WORDBREAK
                    OR DT_EXPANDTABS OR DT_NOPREFIX,
                    DT_RIGHT OR DT_VCENTER OR DT_WORDBREAK OR DT_EXPANDTABS OR DT_NOPREFIX,
                    DT_CENTER OR DT_VCENTER OR DT_WORDBREAK OR DT_EXPANDTABS OR DT_NOPREFIX);
       var s:string;
      begin
        inherited;
        with Rect do
          begin
           left:=left+2;
           top:=top+2;
           Right:=right-5;
          end;
        s:=stringgrid1.Cells[acol,arow];
        if (arow=0) and (acol<stringgrid1.ColCount) then
            begin
            stringgrid1.Canvas.Font.Style:=stringgrid1.Canvas.Font.Style+[fsbold];
            stringgrid1.Canvas.Brush.Color:=stringgrid1.FixedColor;
            stringgrid1.Canvas.FillRect(rect);
            Drawtext(stringgrid1.Canvas.Handle,pchar(s),length(s),rect,AlignFlags[taCenter]);
            end
        else
          if (acol=0 ) and (arow>0) and (arow< stringgrid1.RowCount) then
             begin
             stringgrid1.Canvas.FillRect(rect);
             Drawtext(stringgrid1.Canvas.Handle,PChar(s),length(s),Rect,AlignFlags[taRightJustify]);
             end;end;procedure Tfrmmain.Edit1Change(Sender: TObject);
    var introw:integer;
    begin
      inherited;      with edit1 do
              begin
              introw:=stringgrid1.Row;
              if (stringgrid1.col=3) then
                 stringgrid1.Cells[2,introw]:=edit1.Text
               else
                stringgrid1.Cells[2,introw]:=edit1.Text;
                //visible:=false;
                end;
                //stringgrid1.SetFocus;
    end;procedure Tfrmmain.Edit1PropertiesChange(Sender: TObject);
    var introw:integer;
    begin
      inherited;      with edit1 do
              begin
              introw:=stringgrid1.Row;
              if (stringgrid1.col=3) then
                 stringgrid1.Cells[2,introw]:=edit1.Text
               else
                stringgrid1.Cells[2,introw]:=edit1.Text;
                //visible:=false;
                end;
                //stringgrid1.SetFocus;
    end;end.你看看我写的这些代码,就会明白的,当然有一些你不一定用的上!
      

  3.   

    for i:=1 to Query.RecordCount  do这里错了
     for i:=o to Query.RecordCount-1 doRecordcount是由0开始的在Delphi 中,我所知的计数器都是由0开始的
      

  4.   

    对诸如Query,Table这类数据集部件的操作,最好用While not *.eof 来控制,
    Query.first ;
    while not Query.eof do
    begin    ......    Query.Next;  end; 尽量避免如下这样:
    for i:=1 to Query.RecordCount  do
    begin    ......    Query.Next;  end; 
    如何让网格中的内容靠右显示,标题居中显示:
    在DrawCell事件中:
                Canvas.TextRect (Rect, Rect.Left + 3, Rect.Top + 3,'');
                DrawText(Canvas.handle,Pchar(Cells[ACol,ARow]),Length(Cells[ACol,ARow]),Rect,DT_RIGHT)