DevExpress XPO Persistent Object implement a Save and New method

1.5k views Asked by At

in my Winform Project, i have a simple detail form where we can add new, edit and save persistent Object, until here everything is fine.

Edit Controls are binded by code in the from constructor, and the first new object is also passed by the form constructor when created at first time

Now i would like to implement a Save and New method, but without success

I tried this, assuming that tbVehicule is the object class, theVehicule is my persistent Object and frmVehicule my detail form

  // Form Constructor
    public frmVehicule(tbVehicule theVehicule)
            : this() {
                this.theVehicule = theVehicule;
                // method to bind all controls
                bindingFields();
        }
// Save and new method

private void barBtnSaveNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        this.Validate();
        theVehicule.Save();
        theVehicule = new tbVehicule(theVehicule.Session);

       }

the process should be similar to other ORM like EF or Hibernate

1

There are 1 answers

0
Ait Yahia Idir On

I solved the problem by myself, so I answered my question hope it will help someone, it does not work, because the DataBinding mechanism only works in one direction (form Controls to Object)

if we instantiate new object in our binded object, the controls values don't follows (they are not updated), for this we need to reset a bindings

public void  resetBindings() 
 {
  foreach (Control c in Form1.Controls)

            c.DataBindings.Clear();


            bindingFields();

 }


 public void  bindingFields() 
  {
    txtCode.DataBindings.Add("Text",theVehicule,"vhCode");
      ...
      ..
    txtActifInactif.DataBindings.Add("EditValue", theVehicule, "vhInactif");

  }


private void barBtnSaveNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  {
    this.Validate();
    theVehicule.Save();
    theVehicule = new tbVehicule(theVehicule.Session);
    resetBindings();

   }

with above snipet it's work fine,may be that there are a more elegant way to reset a binding, I opted for redefining bindings, because it is the only one method that I found and that works for the moment. .