德尔福如何使用一个保存按钮保存 3 个数据源



我在将 3 个数据源的所有值保存到带有另一个数据源的 SMDBGrid 中时遇到问题。

我得到了地址ID,联系人ID和关系ID。

这些都不匹配。

问题是我的SMDBGrid有另一个数据源,然后是那3个。我想用一个按钮保存它们。

尝试了很多方法,但找不到好的结果。

这是我现在用于Insert按钮的代码:

procedure TFRelatiebeheer.ToolButton1Click(Sender: TObject);
begin
  DRelatiebeheer.ContactpersonID.Insert;
  DRelatiebeheer.RelationID.Insert;
  DRelatiebeheer.AdressID.Insert;
end;

这是我现在用于保存按钮的代码

  if (DRelatiebeheer.ContactpersonID.State in dsEditModes) then
    if not (DRelatiebeheer.ContactpersonID.State in [dsInsert]) then
    begin
      KJSMDBGrid1.RefreshData;
      KJPanel4.Visible := True;
    end
    else
    begin
      if (DRelatiebeheer.ContactpersonID.State IN dsEditModes) then
        DRelatiebeheer.ContactpersonID.Post;
      if (DRelatiebeheer.AdressID.State IN dsEditModes) then
        DRelatiebeheer.AdressID.Post;
    end;

希望你对我现在正在做的事情有一个好的视野,如果没有,请通知。

我遇到了需要一键保存然后在数据库和网格中刷新的数据源的问题。这意味着当我插入联系人时,需要有一个地址ID和一个关系ID。之后,网格需要重新加载所有数据。

专注于给定的问题根据预期行为(如果可以只发布一个或两个表,或者是否有必要发布所有表),首先要做的是确保可以发布表。您可以为每个表创建一个函数,例如CanAdressIDBePosted:Boolean来检查是否已输入必填字段。表 ContactpersonID 的条件将包含其他条件:输入所需字段和 CanAdressIDBePosted 和 CanRelationIDBePosted。您可以创建一个操作,该操作将绑定在按钮上,其中包含一个 OnUpdate 事件,如下所示:

procedure TForm1.PostActionUpdate(Sender: TObject);
begin
   TAction(Sender).Enabled := CanAdressIDBePosted and CanContactpersonIDBePosted and CanRelationIDBePosted;
   // depending on your requirements (e.g. no need to post RelationID if not entered) it also could be
   TAction(Sender).Enabled := CanAdressIDBePosted or CanContactpersonIDBePosted ;
end;

procedure TForm1.PostActionExecute(Sender: TObject);
begin
   if CanAdressIDBePosted then AdressID.Post; // ensure ID fields will be generated
   if CanRelationIDBePosted  then  RelationID.Post; // ensure ID fields will be generated
   if CanContactpersonIDBePosted then
      begin
         ContactpersonID.FieldByName('AdressID').value := AdressID.FieldByName('ID').Value;
         ContactpersonID.FieldByName('RelationID').value := RelationID.FieldByName('ID').Value;
      end;
   DateSetBoundToTheGrid.Requery;
   // furthor actions you need
end;
Function TForm1.CanAdressIDBePosted:Boolean;
begin
  // example implementation
  Result := (AdressID.State in [dsEdit,dsInsert]) and (not AdressID.FieldByName('NeededField').IsNull);
end;

Function TForm1.CanContactpersonIDBePosted:Boolean;
begin
  // example implementation
  Result := (ContactpersonID.State in [dsEdit,dsInsert]) and (not ContactpersonID.FieldByName('NeededField').IsNull)
             and CanAdressIDBePosted and CanRelationIDBePosted;
end;

如果需要,应创建一个附加操作来取消:

procedure TForm1.CancelActionExecute(Sender: TObject);
begin
    AdressID.Cancel;
    RelationID.Cancel;
    ContactpersonID.Cancel;
end;
procedure TForm1.CancelActionUpdate(Sender: TObject);
begin
   TAction(Sender).Enabled := (AdressID.State in [dsEdit,dsInsert])
                           or (RelationID.State in [dsEdit,dsInsert])
                           or (ContactpersonID.State in [dsEdit,dsInsert]);
end;

总的来说,我不确定您采取的方法是否是可以采取的最好的方法,因为从恕我直言,应该可以将已经存在的关系和地址分配给新生成的联系人,但这将是另一个问题。

这段代码对我来说看起来有些随机。那里应该发生什么?

  if (DRelatiebeheer.ContactpersonID.State in dsEditModes) then 
  // remember this check (1)
    if not (DRelatiebeheer.ContactpersonID.State in [dsInsert]) then
    // this check better written as "...State = dsInsert"
    begin
    // why don't you call DRelatiebeheer.ContactpersonID.Post to save chanegs ?
      KJSMDBGrid1.RefreshData;
      KJPanel4.Visible := True;
    end
    else
    begin
      if (DRelatiebeheer.ContactpersonID.State IN dsEditModes) then
      // you already checked this above (1), why check again ?
        DRelatiebeheer.ContactpersonID.Post;
      if (DRelatiebeheer.AdressID.State IN dsEditModes) then
        DRelatiebeheer.AdressID.Post;
    end;
    // so what about  DRelatiebeheer.RelationID ?

对于我可能推断的,你不必制作任何复杂的if梯子,你只需要从字面上将你的话翻译成德尔菲。您希望保存三个表,然后刷新网格。那就去做吧。

procedure TFRelatiebeheer.SaveButtonClick(Sender: TObject);
begin
  DRelatiebeheer.ContactpersonID.Post;
  DRelatiebeheer.RelationID.Post;
  DRelatiebeheer.AdressID.Post;
  DatabaseConnection.CommitTrans;
  KJSMDBGrid1.RefreshData;
  KJPanel4.Visible := True;  
end;

就像您在其他问题中被告知的那样。

  • 德尔福使用1个保存按钮保存来自不同数据源的所有值
  • 德尔福集 帖子后面板可见

附言。 ToolButton1Click - 普拉斯,请重命名按钮。相信我,当你有 10 个按钮,分别名为 Button1、Button2、...Button10 你永远无法确定每个按钮应该做什么,并且会混合所有内容并制造所有可能的程序逻辑错误。

最新更新