Print Page | Close Window

Update Bound Control in code

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=837
Printed Date: 11-Jun-2026 at 3:07am


Topic: Update Bound Control in code
Posted By: pucsoftware
Subject: Update Bound Control in code
Date Posted: 11-Jun-2008 at 8:48am
I want to update a bound field on my control using code. Otherwise, I am using the LookupDataGrid from DevExpress so that I can build a sophistcated lookup box with filtering, change color of rows based on data, show images, etc. So, this LookupDataGrid is not bound by the PersistenceManager it is simply a lookup list. When the user selects a value from the list I use an event to capture the key to what they selected. This key value is stored in a seperate field that is bound to the PersistenceManager using a text box. If I manually change the value in the text box the data is changed and updated on a save changes command. However, if I select a value from the LookupDataGrid and populate the textbox with the change in code, the PersistenceManager does not save the changes.
Here is my event handler
 
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

private void gridLuDepartment_EditValueChanged(object sender, EventArgs e) {

txtDepartmentKey.Text = gridLuDepartment.EditValue.ToString();

}

Again, visually the value in the textbox is changed but the underlying value in the PersistenceManager is not. How would I do something like this?
 
Darren



Replies:
Posted By: davidklitzke
Date Posted: 12-Jun-2008 at 9:50pm
Darren,
 
In the first case, databinding occurs.  The value in the textbox is consistent with the value of the entity in the Persistence Manager cache.  There is a bidirectional relationship.
 
In the second case, there is no databinding.  Changing the value of the TextBox has no effect on the entity in the Persistence Manager Cache.
 
IN the first case, a setter is fired.
 
In the second case, there is no setter which fires.
 
David


Posted By: davidklitzke
Date Posted: 12-Jun-2008 at 10:23pm
Darren,
 
After reading through my response, I realized I didn't really answer youur question.  In the fist case, there is a series of events that get triggered that eventually causes a setter to get fired , and the setter causes a change in the persistence manager cache.  In the second case, this sequence of events does not occur, and the setter does not get fired.
 
If you want your code changes to cause a change in the database, modify the entity, not the text box.
 
David


Posted By: pucsoftware
Date Posted: 16-Jun-2008 at 8:57am
I understand that I can't change the value by referencing the text box. To be more specific, can you supply me with a small code example on what to reference. I can get a reference to the current record by using:
 
mEmployee = (Employee) bsEmployee.Current;
 
This works okay to read additional data from the fields, but I can't use this method to save any changes. I suspect that you would go through the PersistanceManager object but I don't see how. An example would be helpful.
 
Darren


Posted By: davidklitzke
Date Posted: 16-Jun-2008 at 3:03pm
Darren,
 
Yes.  That will give you the current Employee.  Then, if you want to change the First Name to "Nancy", just write:
 
mEmployee.FirstName = "Nancy"
 
Now, to save all the changes in the application, use something like this:
 

private void SaveAllChanges() {

      if (mPersMgr.HasChanges()) {

        try {

          mPersMgr.SaveChanges();

          MessageBox.Show("Changes saved to database");

        } catch (PersistenceManagerSaveException ex) {

          MessageBox.Show("Errors saving changes to database: \n\n"

            + ex.SaveResult.Exception.ToString());

        }

      } else { MessageBox.Show("No changes to save"); }

    }

If you want just to save the one employee, put the single employee in an EntityList and do a SaveChanges on the EntityList.



Posted By: pucsoftware
Date Posted: 17-Jun-2008 at 8:11am
Okay, that was simple. I was looking for a Save method on the entity object. I guess when you use the PersistanceManager to get the current object you get a reference to the object and the PersistanceManager manages the updates. cool. I didn't think it would do this but it works! Thanks.


Posted By: davidklitzke
Date Posted: 17-Jun-2008 at 8:20am

I'm glad that helped.

It is not so much an issue of getting the current object so much as it is using an overload of SaveChanges that takes an EntityList.



Print Page | Close Window