Original address: Http://forums.lhotka.net/forums/p/3166/21214.aspx
My task is:
For select client, I has a modal form (frmselectclient) with grid. Datasourse for grid is root readonly list of clients (Readonlylistbase). If user can ' t find client in the list, he would as to add new. I Create and show a modal form (frmeditclient) with editable root (businessbase) for add a new Client to DB. This frmeditclient can reuse also to edit existing Client.
My question:
What's the best solution to update readonly clients list on frmselectclient, which a user can see their newly inserted CLI Ents or updated clients info. Now I make "Fullrefresh" data in the root readonly list of clients, but think that's a bad approach :-(
Answer:
The general answer are to use the Observer design pattern.
Set up a Observer to watch for new items being added. The new item is, by definition, an editable root, or were saved through an editable root, which means. A saved event is Raised when the object is saved.
The Observer handles that event, and relays the information to anyone who cares-such as your ROL.
The ROL can then just add that one new item.
The easiest-to-do-is-to-use. NET events as the "Observer".
In your editable root:
public static event EventHandler projectsaved; protected static void onprojectsaved (Project project) { if (projectsaved! = null ) projectsaved ( Project, Eventargs.empty); public override Project Save () {Project result = base . Save (); Onprojectsaved (result); return result; }
The Save () override raises the static event any time, Project object is inserted or updated.
And in your ROL your handle that event:
Privateprojectlist () {project.projectsaved+=NewEventHandler (project_projectsaved); } voidProject_projectsaved (Objectsender, EventArgs e) {Project Project= Sender asProject; if(Project! =NULL) {isreadonly=false; Try { for(inti =0; I < This. Count-1; i++) { if(Thisidea [I]. Id.equals (Project. Id)) {//item exists-update with new dataThisidea [I] =NewProjectInfo (Project. Id, Project. Name); return; } } //Insert Item This. ADD (NewProjectInfo (Project. Id, Project. Name)); } finally{isreadonly=true; } } }
When the event was handled, sender is the new item. You can search the current list for see if that item already exists, and then replace it with an updated version. If it doesn ' t exist you simply add a new item.
Need Help and Design readonlylistbase (Insert, Update, Delete from Readonlylistbase)