New Posts New Posts RSS Feed: Update Bound Control in code
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Update Bound Control in code

 Post Reply Post Reply
Author
pucsoftware View Drop Down
Groupie
Groupie
Avatar

Joined: 15-Apr-2008
Location: United States
Posts: 46
Post Options Post Options   Quote pucsoftware Quote  Post ReplyReply Direct Link To This Post Topic: Update Bound Control in code
    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
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
pucsoftware View Drop Down
Groupie
Groupie
Avatar

Joined: 15-Apr-2008
Location: United States
Posts: 46
Post Options Post Options   Quote pucsoftware Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
pucsoftware View Drop Down
Groupie
Groupie
Avatar

Joined: 15-Apr-2008
Location: United States
Posts: 46
Post Options Post Options   Quote pucsoftware Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down