Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi如何用一个保存按钮保存3个数据源?_Delphi - Fatal编程技术网

Delphi如何用一个保存按钮保存3个数据源?

Delphi如何用一个保存按钮保存3个数据源?,delphi,Delphi,我在使用另一个DataSource将3个数据源中的所有值保存到SMDBGrid时遇到问题 我有地址,联系人和关系 这些都不匹配 问题是我的SMDBGrid有另一个数据源,而不是那3个。 我想用一个按钮保存它们 尝试了很多方法,但没有找到好的结果 这是我现在用于插入按钮的代码: procedure TFRelatiebeheer.ToolButton1Click(Sender: TObject); begin DRelatiebeheer.ContactpersonID.Insert; D

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

我有地址,联系人和关系

这些都不匹配

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

尝试了很多方法,但没有找到好的结果

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

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;
希望你对我现在正在做的事情有一个好的了解,如果没有请通知我

我遇到了数据源的问题,这些数据源需要在单击一次后保存,然后在数据库和网格中刷新。 这意味着,当我插入一个联系人时,需要有一个ADRESID和一个与之耦合的RelationID。
之后,网格需要重新加载所有数据。

我觉得这段代码有点随机。那里会发生什么

  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梯形图,你只需将你的文字逐字翻译成Delphi即可。您需要保存三个表,然后刷新网格。那就去做吧

procedure TFRelatiebeheer.SaveButtonClick(Sender: TObject);
begin
  DRelatiebeheer.ContactpersonID.Post;
  DRelatiebeheer.RelationID.Post;
  DRelatiebeheer.AdressID.Post;

  DatabaseConnection.CommitTrans;

  KJSMDBGrid1.RefreshData;
  KJPanel4.Visible := True;  
end;
就像你在其他问题中被告知的那样


PS.
工具按钮1单击
-plase,重命名按钮。相信我,当你有10个名为Button1,Button2,…Button10的按钮时,你永远不会确定每个按钮都应该做什么,并且会混合所有的东西,造成所有可能的程序逻辑错误。

我觉得这段代码有点随机。那里会发生什么

  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梯形图,你只需将你的文字逐字翻译成Delphi即可。您需要保存三个表,然后刷新网格。那就去做吧

procedure TFRelatiebeheer.SaveButtonClick(Sender: TObject);
begin
  DRelatiebeheer.ContactpersonID.Post;
  DRelatiebeheer.RelationID.Post;
  DRelatiebeheer.AdressID.Post;

  DatabaseConnection.CommitTrans;

  KJSMDBGrid1.RefreshData;
  KJPanel4.Visible := True;  
end;
就像你在其他问题中被告知的那样


PS.
工具按钮1单击
-plase,重命名按钮。相信我,当你有10个名为Button1,Button2,…Button10的按钮时,你永远不会确定每个按钮都应该做什么,并且会混合所有的东西,造成所有可能的程序逻辑错误。

关注给定的问题 根据预期的行为(如果可能只发布一个或两个表,或者是否有必要发布所有表),首先要做的是确保可以发布这些表。您可以为每个表创建一个函数,例如canadresidbeposed:Boolean,以检查是否已输入必需的字段。ContactpersonID表的条件将包含其他条件:输入所需字段,并记录CanadResidedReported和CanRelationdReported。您可以创建一个操作,该操作将与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;

总的来说,我不确定您采取的方法是否是最好的,因为从IMHO提供的结构来看,应该可以将现有的关系和地址分配给新生成的联系人,但这将是另一个问题。

关注给定的问题 根据预期的行为(如果可能只发布一个或两个表,或者是否有必要发布所有表),首先要做的是确保可以发布这些表。您可以为每个表创建一个函数,例如canadresidbeposed:Boolean,以检查是否已输入必需的字段。ContactpersonID表的条件将包含其他条件:输入所需字段,并记录CanadResidedReported和CanRelationdReported。您可以创建一个操作,该操作将与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;

总的来说,我不确定您采取的方法是否是最好的,因为根据IMHO提供的结构,应该可以将现有的关系和地址分配给新生成的联系人,但那将是另一个问题。

不是同一个问题吗?@AgustinSeifert是的,但不知道如何正确解决它。我是德尔福的新手,所以我不太了解它。希望有人能让我更容易尝试。不知道发生了什么/应该发生什么,很难回答。如果数据集绑定到网格,是否存在刷新问题?只是缺少RelationID的帖子?由于dsEditModes/dsInsert导致的不一致。数据集之间是否存在导致发布时需要特殊序列以获取其他表中作为引用所需的AutoID值的关系,如果存在这种关系,则在发布前是否已有机制?@bummi我发现数据源存在问题,需要单击一次保存,然后在数据库和网格中刷新。这意味着当我插入联系人时,需要有一个ADRESID和一个与之耦合的RelationID。之后,网格需要重新加载所有数据。在我看来,问题是你已经接受了一个问题的答案,而这个答案并不包含完整答案所需的信息。你可以考虑提炼你的问题并添加丢失的信息。评论不是添加基本信息的地方。这将增强问题,并使其对未来用户更感兴趣,这可能会导致对您的问题进行向上投票,并得出涵盖整个问题的答案。我很清楚答案是什么样子的,但是在堆栈溢出问题上不建议回答模糊的问题。不是同一个问题吗???@AgustinSeifert是的,但不知道如何正确解决它。我是delphi的新手,所以我不太了解它。希望有人能让我更容易尝试。不知道发生了什么/应该发生什么,很难回答。这是r的问题吗