简体   繁体   English

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

[英]Delphi how to save 3 datasources with one save button?

I got a problem with saving all values from 3 datasources into a SMDBGrid with another datasouce. 我将3个数据源中的所有值保存到另一个数据源的SMDBGrid中时遇到问题。

I got AdressID, ContactpersonID and RelationID. 我有AdressID,ContactpersonID和RelationID。

Those all dont match each others. 那些都不匹配。

The problem is that my SMDBGrid has another datasource then those 3. I wanna save them with one button. 问题是我的SMDBGrid有另一个数据源,然后是那三个。我想用一个按钮保存它们。

Tried many ways but can't find a good result. 尝试了许多方法,但找不到很好的结果。

this is the code i use right now for my Insert button: 这是我现在用于“ Insert按钮的代码:

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

This is the code i use for my save button right now 这是我现在用于保存按钮的代码

  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;

Hope you have a good sight for what I am doing right now, if not please notify. 希望您对我现在正在做的事情有个很好的了解,否则请通知。

I got the problem with the datasources that need to be saved on 1 click and then be refreshed in the database and in the Grid. 我遇到了需要一键保存然后在数据库和网格中刷新的数据源的问题。 That means that when I insert a Contactperson there needs to be a AdressID and a RelationID coupled with it. 这意味着当我插入一个Contactperson时,需要有一个AdressID和一个RelationID与其耦合。 After that the grid needs to reload all of the data. 之后,网格需要重新加载所有数据。

This code just looks somewhat random to me. 这段代码对我来说似乎有些随机。 What SHOULD happen there ? 那应该发生什么?

  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 ?

For what i may deduce, you don't have to make any complex if-ladders, you just have to literally translate your words to Delphi. 对于我可能得出的结论,您不必编写任何复杂的if-ladder,而只需将字面意义翻译为Delphi。 You want to save three tables and then refresh the grid. 您要保存三个表,然后刷新网格。 Then just do it. 然后就做吧。

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

  DatabaseConnection.CommitTrans;

  KJSMDBGrid1.RefreshData;
  KJPanel4.Visible := True;  
end;

Just like you was told in your other questions. 就像在其他问题中被告知您一样。

PS. PS。 ToolButton1Click - plase, DO rename the buttons. ToolButton1Click ,请重命名按钮。 Believe me when you have 10 buttons named Button1, Button2, ...Button10 you would never be sure what each button should do and would mix everything and make all possible program logic errors. 相信我,当您有10个名为Button1,Button2,... Button10的按钮时,您将永远不确定每个按钮应该做什么,并且会混淆所有内容并导致所有可能的程序逻辑错误。

Focusing on the given problem Depending on the intended behavior (should it be possible posting only one or two table(s) or is it necessary to post all tables) the first thing to do would be to ensure that the tables can be posted. 专注于给定的问题根据预期的行为(应该仅发布一个或两个表,还是必须发布所有表),第一件事是确保可以发布表。 You coulds create a function for each table eg CanAdressIDBePosted:Boolean to check if required fields are already entered. 您可以为每个表创建一个函数,例如CanAdressIDBePosted:Boolean以检查是否已输入必填字段。 The condition of the table ContactpersonID would contain additional conditions: needed fields are entered AND CanAdressIDBePosted AND CanRelationIDBePosted. 表ContactpersonID的条件将包含其他条件:输入所需字段AND CanAdressIDBePosted和CanRelationIDBePosted。 You could create an Action which would be bound on your button with an OnUpdate event which could look like this: 您可以创建一个动作,该动作将与一个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;

An addidtional Action should be created to cancel if needed: 如果需要,应创建一个附加动作来取消:

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;

In general I am not sure if the approach you took ist the best which can be taken, since from the structure given IMHO it should be possible to assign already existing relations and adresses to new generated contactpersons, but that would be another question. 通常,我不确定您采用的方法是否最佳,因为从恕我直言的结构中,应该可以将现有的关系和地址分配给新生成的联系人,但这将是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM