Scene: A main window, you can select a record in the list (DataGridView) editing, open an editing window (non-modal window), the editing window will need to refresh the parent window, because the editing window is modeless window, if you open more than one window, and are all editing the same piece of data, After a window is saved (and closed), you need to notify other open windows "data has changed and needs to be refreshed"
First, refresh the parent window, if it is the modal window that opens the edit window, then you can implement the following (pseudo code) like this:
New= The ID of the selected data row ; if (frm. ShowDialog () = = DialogResult.OK) { updatethisform ();}
The non-modal window is form.show (); Since the method is void decorated and therefore cannot be implemented as above, an event can be exposed in the editing window class, and when the parent window is new, the edit window can register the event and then edit the window if it saves the effect that the event method can be invoked to reach the notification.
Here is an example, the main window has a DataGridView control, the data binding is a collection of person, the person entity class has the Id,name property, select a row and click Edit, you can open the editing interface; The edit interface has a text box that displays the name of the person being edited, There is a Save button, click Save to update the modified name to the person collection (here the person collection is saved in the read by XML serialization and deserialization implementation)
Main Window Core code:
Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { if(DataGridView1.SelectedRows.Count = =1) { intPersonId = (int) datagridview1.selectedrows[0]. cells["Id"]. Value; Form2 frm=NewForm2 (); Frm.personid=personId; frm. Updateparentevent+=frm_updateparentevent; frm. Show (); } } Private voidfrm_updateparentevent () {loaddata (); } Private voidForm1_Load (Objectsender, EventArgs e) {LoadData (); } Private voidLoadData () {List<Person> personlist = xmlserializehelper.deserializeobject<list<person>> ("Persons.xml"); Datagridview1.datasource=personlist; }
View Code
Edit Window Core code:
Public Partial classForm2:form { Public intpersonId; /// <summary> ///refreshes the events of the parent window/// </summary> Public EventAction updateparentevent; PrivatePerson p =NULL; PrivateList<person>persons; PublicForm2 () {InitializeComponent (); } Private voidForm2_load (Objectsender, EventArgs e) {Persons= Xmlserializehelper.deserializeobject<list<person>> ("Persons.xml"); P= persons. Where (PS = PS. Id = =personId). Singleordefault (); if(P! =NULL) {txtName.Text=P.name; } } Private voidBtnsave_click (Objectsender, EventArgs e) { if(P! =NULL) {P.name=txtName.Text; Xmlserializehelper.serializeobject (Persons,"Persons.xml"); Updateparentevent?. Invoke (); //get all open windows varOpenForms =application.openforms; Type Thistype= This. GetType (); This. Close (); foreach(varIteminchopenforms) {Type ItemType=item. GetType (); //if both are instances of the current window's class, but not the current instance (proving that multiple windows are open) if(ItemType = = Thistype &&!)Object. ReferenceEquals (Item, This)) { intItempersonid = (int) Itemtype.getfield ("personId"). GetValue (item); //prove that the edit is the same record, you need to notify other Windows to refresh the page if(Itempersonid = = This. PersonId) {MethodInfo MInfo= Itemtype.getmethod ("Changehandle", BindingFlags.NonPublic |bindingflags.instance); MInfo?. Invoke (Item,NULL); } } } } } Private voidChangehandle () {if(MessageBox.Show ("Other Windows Modify this data and need to reload","Tips", messageboxbuttons.ok,messageboxicon.information) = =DialogResult.OK) {//Reload DataForm2_load ( This,NULL); } } }
View Code
Test:
Here are two editing windows open, and all editing the same data, when editing one of the name, and save, another hint needs to be refreshed
The example uses the Application.openforms, gets all the currently open windows, traverses and obtains their "type" (type, the same as the same) through reflection, if the type is the same as the type of the current window, and is not the current window, and the same data is being edited. The reflection gets the method and is called to achieve the effect of the notification.
Winform multiple Windows edit the same data synchronization implementation