多谢各位帮忙了在该daagrid的DrawColumnCell事件的代码如下:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Column.Index = 0 then
  with DBGrid1.Canvas do begin
    FillRect(Rect);
    TextOut(Rect.Left+2, Rect.Top+2, IntToStr(DBGrid1.DataSource.DataSet.RecNo));
  end;
end;运行后在这一列的值却总是固定的值,请教各位大虾,该如何解决呢?
很急,解答完了,立即送分!

解决方案 »

  1.   

    用计算字段就可以了值等于table.recno+1
      

  2.   

    再sql中加一栏no.
    select rownum from table
      

  3.   

    to  linzhisong(無聊):     你说的值等于table.recno+1,能否解释清楚,又该如何实现呢?
      

  4.   

    添加一个计算字段id然后再table的onCalcFields事件里加
    table.fieldbyname('id').asinteger:=table1.recno+1
    就可以了
      

  5.   

    补充无聊:
      无聊说得对,可这位仁兄好像刚学DELPHI,计算字段是在TTable里面夹的而不是DBGrid,
    1.双击TTable
    2.在出现的窗体中点右键,选择添加新字段
    3.在弹出对话框中选择属性.
      

  6.   

    不知道楼主是什么情况,我的确可以执行的。如下:
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if Column.Index = 5 then
      with DBGrid1.Canvas do begin
        FillRect(Rect);
        TextOut(Rect.Left+2, Rect.Top+2, IntToStr(DBGrid1.DataSource.DataSet.RecNo));
      end;
    end;
    DBGrid  object DBGrid1: TDBGrid
        Left = 16
        Top = 16
        Width = 657
        Height = 265
        DataSource = DataSource1
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'MS Sans Serif'
        TitleFont.Style = []
        OnDrawDataCell = DBGrid1DrawDataCell
        OnDrawColumnCell = DBGrid1DrawColumnCell
        Columns = <
          item
            Expanded = False
            FieldName = 'ID'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'username'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'userpass'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'userpopedom'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'userdq'
            Visible = True
          end
          item
            Expanded = False
            Title.Caption = 'RecNo'
            Visible = True
          end>
      end----------------------------------------------------------
    等待,如沙漠行舟,痛苦万分
      

  7.   

    运行后在这一列的值却总是固定的值?什么意思?没问题呀!procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    if Column.Title.Caption='NAME' then
          begin
            DBGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top
                + 2,inttostr(DBGrid1.DataSource.DataSet.RecNo));
          end;    Application.ProcessMessages;
    end;
      

  8.   

    运行后在这一列的值却总是固定的值?什么意思?没问题呀!procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    if Column.Title.Caption='NAME' then
          begin
            DBGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top
                + 2,inttostr(DBGrid1.DataSource.DataSet.RecNo));
          end; 
    end;