如何在DBGrid1中加入序号 请高手指点 谢谢

解决方案 »

  1.   

    加一个计算字段
    当增加新记录的时候 Query.FieldByName(计算字段名).asInteger:=TStringGrid(DBgrid).Row;
      

  2.   

    却时是会把当前条的recno默认为-1
    你可以
      with DataSet do
      begin
        if RecNo > 0 then
          FieldByName('ID').AsInteger := RecNo
        else
          FieldByName('ID').AsInteger := 1;
      end;就是增加一个字段
      

  3.   

    楼上说的都不方便
    加一个计算字段
    在这个计算字段的OnGetText事件写上:
    Text:=IntToStr(Query.RecNo);
      

  4.   

    楼上所说的也不行啊!
    因为Query.RecNo的默认为-1
      

  5.   

    procedure TFrmEmpMan.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
      with DBGrid1.DataSource.DataSet do
      begin
        if DataCol = 0 then//第一列
         DBGrid1.Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, IntToStr(RecNo));
      end; 
    end;
      

  6.   

    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;
      

  7.   

    太好了!我又長了一點見識了!謝謝smiler007高手!
      

  8.   

    最好自己写一个:protected
      procedure SetColumnAttributes; override;  //设置左边那个固定栏的宽....procedure TDBGroupGridEh.SetColumnAttributes;
    begin
      inherited;  if (dgIndicator in Options) then
        ColWidths[0] := 40;end;其它的按楼上的楼上的代码.
      

  9.   

    zdf_zhang(杀倭少将)和smiler007(笑一笑)的代码都对呀。
      

  10.   


    自己写的一个控件中显示行号的一段代码
    procedure RGrid.ShowRowNo;
    var i,iCount:Integer;begin
      if FixedCols<1 then Exit;//如果没有序号区则不用显示行号
      iCount:=1;
       for i:=FixedRows to RowCount-1 do
       begin
         Cells[0,i]:=IntToStr(iCount);
         iCount:=iCount+1;
       end;
    end;
      

  11.   

    我忘了一个问题:
    应该用ADOQuery
    加一个计算字段
    在这个计算字段的OnGetText事件写上:
    Text:=IntToStr(Query.RecNo);
      

  12.   

    各位老大
    取RecNo的值,每一条记录都为-1吖
    还是未能实现显示序号的目的吖