使用控件有:
   DBNavigator1: TDBNavigator;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    ADOTable1: TADOTable; 
还有多个DBEDIT,每个字段对应一个,DBGrid用于浏览数据,DBEDIT用于编辑修改数据!但在更改DBGrid中数据的过程中如果误(操作)移动了DBGrid的滚动条,数据则被修改,如若不想这样(不想在移动滚动条时保存数据)该怎样做?
各位大侠请支招!在线等待

解决方案 »

  1.   


    locktype 设为ltBatchOptimistic
    这样便不会保存
    除非运行
    ADOTable1.UpdateBatch;才真正保存。
      

  2.   

    大侠!测试中有问题!
    但是这样会增加并保存在编辑的字段的编辑前数据
    例如设置Adotable的locktype 为ltBatchOptimistic后;
    在编辑字段“姓名”(编辑前为1,编辑后为2)时,移动滚动条,虽不会使现在的这条记录变为2,但是会在DBGrid的末尾添加一条姓名为1记录
      

  3.   

    DBNavigator1: TDBNavigator;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        ADOTable1: TADOTable; 
    还有多个DBEDIT,每个字段对应一个,DBGrid用于浏览数据,DBEDIT用于编辑修改数据!注:Adotable的locktype已设为了:ltBatchOptimistic-----------------全代码如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, ADODB, ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Mask;type
      TForm1 = class(TForm)
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
        DBNavigator1: TDBNavigator;
        ADOTable1: TADOTable;
        Button1: TButton;
        DBEdit1: TDBEdit;
        DBEdit2: TDBEdit;
        DBEdit3: TDBEdit;
        procedure Button1Click(Sender: TObject);
        procedure ADOTable1BeforePost(DataSet: TDataSet);
        procedure FormCreate(Sender: TObject);
        procedure DBEdit1Change(Sender: TObject);
      private
        { Private declarations }
        FIsPost: boolean;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
     dbgrid1.Options:=[dgTitles,dgcolLines,dgRowLines,dgCancelOnExit ];
     if (adotable1.State=dsEdit) or (adotable1.State=dsInsert)  then
      begin
       ADOTable1.post;
       FIsPost:=false;
      end
     else
       showmessage('不能保存'); //FIsPost:=true;
    end;procedure TForm1.ADOTable1BeforePost(DataSet: TDataSet);
    begin
      if FIsPost=true then
        begin
         FIsPost := False;     
         adotable1.Post;    end
      else
        adotable1.Cancel;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
        FIsPost:=false;
    end;procedure TForm1.DBEdit1Change(Sender: TObject);
    begin      FIsPost := False;      //adotable1.Cancel;
    end;end.但是这样会增加并保存在编辑的字段的编辑前数据
    例如设置Adotable的locktype 为ltBatchOptimistic后;
    在编辑字段“姓名”(编辑前为1,编辑后为2)时,移动滚动条,虽不会使现在的这条记录变为2,但是会在DBGrid的末尾添加一条姓名为1记录
      

  4.   

    拜托,把beforepost里的东西去掉,把FIsPost这个变量去掉,
    post完以后,
    没有用
    ADOTable1.UpdateBatch;是不会保存到数据库的,虽然界面上的数据已经改动。
      

  5.   

    是这样子啊
    那还是用你那个FIsPost 变量控制吧,也不用把Adotable的locktype设成ltBatchOptimistic了。
    procedure TForm1.ADOTable1BeforePost(DataSet: TDataSet);
    begin
      if not FIsPost then
      begin
        adotable1.Cancel;
        adotable1.edit;
      end;
    end;
    //注意,beforepost里不要再加post了。
    你之前说“会在DBGrid的末尾添加一条姓名为1记录”就是这个原因。