0

I have an object called Tel, which is bound to three controls as following.

  this.txtTelName.DataBindings.Add("Text", tel, "T_Name", false, DataSourceUpdateMode.OnPropertyChanged);
  this.chkActive.DataBindings.Add("Checked", tel, "T_Active", true, DataSourceUpdateMode.OnPropertyChanged);
  this.txtNotes.DataBindings.Add("Text", tel, "T_Notes", true, DataSourceUpdateMode.OnPropertyChanged);

This prompt all the values that the object has into the controls successfully.

I need to implement a Next button, which allow the users to save the values on the controls by saving the tel object and then create tel object with a new instance and cleaning all the controls. So the users can add a new record.

This is the next button code:

   private void btnNext_Click(object sender, EventArgs e)
   {
     BllTel.Save(tel); //Saves the Tel entity 

     this.tel = new Model.DatabaseModels.Tel();
     tel.T_Active = BusinessLogic.Enums.StatusCodes.Active;

     txtTelName.Text = "";
     txtNotes.Text = "";
   }

Until here everything is OK, but after the user goes for the second time to add, the tel object does not bind the values they write in the controls, having the T_Name and T_Notes as nulls.

Notes: I've noticed as soon as the btnNext_Click method code block ends (on dubuggin), it goes through all the entities in the database (i see it, when debugging through, it passes through all the properties defined in the EDMX designer, and i can see it fetches all the values avaliable in the table).

1 Answer 1

1

It of course works that way. When you change the instance of tel, it's no longer bound to your other controls. So you should define some method to bind data between them and re-call this method:

public void BindData(object tel){
   if(txtTelName.DataBindings["Text"] != null) 
      txtTelName.DataBindings.Remove(txtTelName.DataBindings["Text"]);
   txtTelName.Add("Text", tel, "T_Name", false, DataSourceUpdateMode.OnPropertyChanged);
   if(chkActive.DataBindings["Checked"] != null)
      chkActive.DataBindings.Remove(chkActive.DataBindings["Checked"]);       
   chkActive.DataBindings.Add("Checked", tel, "T_Active", true, DataSourceUpdateMode.OnPropertyChanged);       
   if(txtNotes.DataBindings["Text"] != null)
     txtNotes.DataBindings.Remove(txtNotes.DataBindings["Text"]);       
   txtNotes.DataBindings.Add("Text", tel, "T_Notes", true, DataSourceUpdateMode.OnPropertyChanged);
}
//then use it like this:
tel = new Model.DatabaseModels.Tel();
tel.T_Active = BusinessLogic.Enums.StatusCodes.Active;
BindData(tel);
3
  • Same results, the controls keeps the binding even though i create a new instance of the object. Maybe my question is not clear enough :(.
    – DJ.
    Oct 29, 2013 at 15:50
  • It looks that the bindings are set to a specific object instance. I change a little bit of your code and everything works perfect, Thanks The binding needs to be cleared and recreated: if (txtTelName.DataBindings["Text"] != null) { this.txtTelName.DataBindings.Clear(); this.txtTelName.DataBindings.Add("Text", telData, "T_Name", false, DataSourceUpdateMode.OnPropertyChanged); }
    – DJ.
    Oct 29, 2013 at 15:57
  • 1
    @Djavier89 don't use DataBindings.Clear(), it will remove all other Bindings if you have, just use DataBindings.Remove, if you don't know how, see my update.
    – King King
    Oct 29, 2013 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.