下面我的dbgrid中显示的代码,如何将记录号加到dbgrid 列中,谢谢了!
qq1:=tadoquery.Create(self);
  qq1.Connection:=conn;
  with qq1 do
  begin
    close;
    sql.Clear;
    sql.Add('select id,haoma as 号码,guishu as 归属地,tongxunming as 通讯公司 from haoma ');
    open;
  end;
datasource1.DataSet:=qq1;
  dbgrid1.DataSource:=datasource1;

解决方案 »

  1.   

    {覆盖DrawCell事件,显示行序号 NO }
    procedure TDBGridEh.DrawCell(ACol:Integer;ARow:Integer;ARect:TRect;AState:TGridDrawState);
    var
      i,DBRow,lLeft:integer;
      iCol,iRow:integer;  //全局变量
    begin
      inherited;  {是否开启显示行号功能!}
      if fShowRecNo=True then
      begin
        if Assigned(DataLink) and
            DataLink.Active then //没有open
        begin
          if (ARow>=1) and
              (ACol=0) then{行标头之内}
          begin
            i             := 0;//偏移值
            lLeft         := 0;//居中显示
            DBRow         := self.DataSource.DataSet.RecNo;//当前数据集所在的行标 rec no
            if DataSource.DataSet.RecNo=-1 then{新增记录状态}
            begin
              DBRow := DataSource.DataSet.RecordCount+1;//当前行标取记录总数+1
            end;
            i      := DBRow - self.Row ;//计算表格TopRow和数据集所在行的偏移值(显示实际数据集行号)
            lLeft  := (ARect.Right - ARect.Left) div 2 - Canvas.TextWidth(IntToSTr({DataLink.ActiveRecord}ARow+i)) div 2;//居中显示
            Canvas.TextRect(ARect,ARect.Left+lLeft,ARect.Top,IntToSTr({DataLink.ActiveRecord}ARow+i));//输出行号
          end;
        end;
      end;
    end;
      

  2.   

    1、在DBGRID中增加一列,列名的抬头比如说是'序号'
    2、代码:procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
       if DataSource1.DataSet.RecNo > 0 then
      begin
        if WideUpperCase(Column.Title.Caption) = '序号' then
          DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top, IntToStr(DataSource1.DataSet.RecNo));
      end;
    end;