问题:
   在dbgrid有两个字段是连在table上的.还有一个字段是我用加上的column.
   (就是数据表中只有两个字段.dbgrid中有三个columns.),如果我要在dbgrid中输入数据.dbgrid是连在表上.但是第三个column没有办法输入.以前我有看过有人就有用过这种办法.但是我没有办法做到,想请高手帮忙写这段代码.

解决方案 »

  1.   

    因为你操作的DBGRID是一个数据集,所以你只能操作这两个字段,
      

  2.   

    在DrawColumnCell事件里(Sender As TDBGrid).Canvas.TextRect(Rect, 0, Rect.Top + 2, 'aaaa');
      

  3.   

    呵呵,不好意思,刚刚仔细看了看,你说的是在dbgrid里输,我说的是显示
    如果dbgrid的column没有和子段关联,是不能直接输入的
    但是可以用我的方法来显示  :)
      

  4.   

    不连库肯定没办法输入的,那你用StringGrid好了
      

  5.   

    那你给table加一个计算字段,用新建的cloumn连上看行吗.
      

  6.   

    那如何才可以在dbgrid新增的column中顯示另一個表的字段值
      

  7.   

    那个我不是说了吗?在DrawColumnCell事件里重画就行了
      

  8.   

    object Form1: TForm1
      Left = 192
      Top = 107
      Width = 696
      Height = 480
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object DBGrid1: TDBGrid
        Left = 160
        Top = 80
        Width = 417
        Height = 249
        DataSource = DataSource1
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'MS Sans Serif'
        TitleFont.Style = []
        OnDrawColumnCell = DBGrid1DrawColumnCell
        Columns = <
          item
            Expanded = False
            FieldName = 'age'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'card'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'name'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'sex'
            Visible = True
          end
          item
            Expanded = False
            FieldName = 'name'
            Title.Caption = 'phone'
            Visible = True
          end>
      end
      object DataSource1: TDataSource
        DataSet = ADOTable1
        Left = 64
        Top = 40
      end
      object ADOTable1: TADOTable
        Active = True
        Connection = ADOConnection1
        CursorType = ctStatic
        TableName = '表2'
        Left = 120
        Top = 48
      end
      object ADOConnection1: TADOConnection
        Connected = True
        ConnectionString =
          'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security In' +
          'fo=False;Initial Catalog=gg;Data Source=YZ89'
        LoginPrompt = False
        Provider = 'SQLOLEDB.1'
        Left = 48
        Top = 112
      end
      object ADOTable2: TADOTable
        Active = True
        Connection = ADOConnection1
        CursorType = ctStatic
        TableName = '表1'
        Left = 80
        Top = 328
      end
    end
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var str:string;
    begin
       if  Column.Title.Caption = 'phone' then
       begin
          ADOTable2.Locate('name',Column.Field.Value,[loCaseInsensitive]);
          str:=ADOTable2['phone'];
         (Sender As TDBGrid).Canvas.TextRect(Rect, Rect.Left + 20, Rect.Top + 2, str);
       end;
    end;