我想实现的功能是:1,当表单运行完后,鼠标点dbgrid上哪行,在edit中显示该行的所有数据,然后可以实行修改操作;2,若用dbedit并且不用dbnavigator,咋样实现修改操作,最好能有案例代码????? 请高手们帮帮忙!!!!

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        ADOTable1: TADOTable;
        Edit1: TEdit;
        Button1: TButton;
        Edit2: TEdit;
        Edit3: TEdit;
        procedure DataSource1DataChange(Sender: TObject; Field: TField);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
    begin   edit1.Text := (sender as TDataSource).DataSet.FieldByName('username').AsString;
       edit2.Text := (sender as TDataSource).DataSet.FieldByName('passwd').AsString
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
       with  adotable1 do
       begin
         if not Active then
           Active := true;
         edit;
         FieldByName('passwd').Value := Edit3.Text;
         Post;
       end;
    end;end.窗体文件如下:
    object Form1: TForm1
      Left = 192
      Top = 110
      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 = 0
        Top = 330
        Width = 688
        Height = 120
        Align = alBottom
        DataSource = DataSource1
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'MS Sans Serif'
        TitleFont.Style = []
      end
      object Edit1: TEdit
        Left = 40
        Top = 32
        Width = 121
        Height = 21
        TabOrder = 1
        Text = 'Edit1'
      end
      object Button1: TButton
        Left = 240
        Top = 144
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 2
        OnClick = Button1Click
      end
      object Edit2: TEdit
        Left = 40
        Top = 64
        Width = 121
        Height = 21
        TabOrder = 3
        Text = 'Edit2'
      end
      object Edit3: TEdit
        Left = 40
        Top = 96
        Width = 121
        Height = 21
        TabOrder = 4
        Text = 'Edit3'
      end
      object DataSource1: TDataSource
        AutoEdit = False
        DataSet = ADOTable1
        OnDataChange = DataSource1DataChange
        Left = 216
        Top = 48
      end
      object ADOTable1: TADOTable
        Active = True
        ConnectionString = 
          'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security In' +
          'fo=False;Initial Catalog=**;Data Source=.'
        CursorType = ctStatic
        TableName = 'userInfotb'
        Left = 296
        Top = 56
      end
    end
      

  2.   

    关键就是在grid的DataChange事件写上代码,让记录实时显示。
    procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
    begin
       edit1.Text := (sender as TDataSource).DataSet.FieldByName('username').AsString;
       edit2.Text := (sender as TDataSource).DataSet.FieldByName('passwd').AsString
    end;楼上给的就可以了