Delphi中
当一个数据表格控件(DBGrid)与一个 空的 数据集关联
一般在这个数据表格控件获得焦点的时候,
系统会自动插入一条新的记录以供编辑
似乎是DataSource.AutoEdit控制的,现在的问题是:自动插入的这个记录没有意义,我也不需要有什么办法:
既可以让数据集在获得焦点时自动进入编辑状态
又可以在数据集为空时不插入新的记录?
(只需要编辑现有的记录,不允许手工插入新的记录)

解决方案 »

  1.   

    在DataSet的OnBeforePost事件中判断字段的值。
      

  2.   

    如果你所要字段值为空就abort
      

  3.   

      那你就在 onBeforeinsert   裏加一個 abort  ,呵呵
      

  4.   

    你自己都说对了:DataSource的AutoEdit设为False即可,如果确实需要编辑就调用DataSet.Edit......以下为Delphi帮助对AutoEdit属性的解释:
    Determines if a data source component automatically calls a dataset抯 Edit method when a data-aware control associated with the data source receives focus.Delphi syntax:property AutoEdit: Boolean;C++ syntax:__property bool AutoEdit = {read=FAutoEdit, write=FAutoEdit, default=1};DescriptionAutoEdit is true by default. If AutoEdit is true, then when a user attempts to modify the data displayed by the control the data source calls the underlying dataset抯 Edit method.Set AutoEdit to false to protect data from unintentional modification. Even if AutoEdit is false, an application can explicitly call a dataset抯 Edit method to permit data modification.
      

  5.   

    在數據集剛打開的時候 就把狀態設置為dsBrowse ,這是瀏覽狀態,不能編輯的.
      

  6.   

    BeforePost(DataSet: TDataSet);事件中举例
        if (DataSet.FieldByName('NIAN').AsInteger<1900) then
        begin
          DataSet.Cancel;
        end;