The Winform window is used to edit the implementation of the same data synchronization in multiple windows.

Source: Internet
Author: User

The Winform window is used to edit the implementation of the same data synchronization in multiple windows.

Scenario: in the main window, you can select a record in the list (DataGridView) to open an edit window (non-modal window). After the edit window is saved, you need to refresh the parent window, because the editing window is a non-modal window, if multiple windows are opened and the same data is edited, after a window is saved (and closed, you need to notify another window that is being opened: "data has changed and needs to be refreshed"

First, refresh the parent window. If the edit window is opened and the modal window is opened, the implementation can be similar to the following (pseudo code ):

FormEdit frm = new FormEdit (); frm. EditId = id of the selected data row; if (frm. ShowDialog () = DialogResult. OK) {UpdateThisForm ();}

The non-modal window is Form. show (); because this method is modified by void, it cannot be implemented as above. In this case, an event can be published in the edit window class. After the new edit window in the parent window, you can register this event, and then call this event method to achieve the notification effect if it is saved in the edit window.

The following example shows a DataGridView control in the main window. Data Binding is a set of persons. The Person entity class has the Id and Name attributes. Select a row and click Edit to open the editing interface; on the editing page, there is a text box showing the Name of the Person to be edited and a Save button, click Save to update the modified Name to the Person set (here, the Person set is saved for reading through Xml serialization and deserialization)

 

 

Core code of the main window:

public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            if (dataGridView1.SelectedRows.Count == 1)            {                int personId = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;                Form2 frm = new Form2();                frm.personId = personId;                frm.UpdateParentEvent += Frm_UpdateParentEvent;                frm.Show();            }        }        private void Frm_UpdateParentEvent()        {            LoadData();        }        private void Form1_Load(object sender, EventArgs e)        {            LoadData();        }        private void LoadData()        {            List<Person> personList = XmlSerializeHelper.DeserializeObject<List<Person>>("persons.xml");            dataGridView1.DataSource = personList;        }
View Code

 

Edit the core code of the window:

Public partial class Form2: Form {public int personId; // <summary> // refresh the event in the parent window /// </summary> public event Action UpdateParentEvent; private Person p = null; private List <Person> persons; public Form2 () {InitializeComponent ();} private void Form2_Load (object sender, EventArgs e) {persons = XmlSerializeHelper. deserializeObject <List <Person> ("persons. xml "); p = persons. where (ps => ps. id = pe RsonId). SingleOrDefault (); if (p! = Null) {txtName. Text = p. Name;} private void btnSave_Click (object sender, EventArgs e) {if (p! = Null) {p. Name = txtName. Text; XmlSerializeHelper. SerializeObject (persons, "persons. xml"); UpdateParentEvent ?. Invoke (); // obtain all opened windows var openForms = Application. openForms; Type thisType = this. getType (); this. close (); foreach (var item in openForms) {Type itemType = item. getType (); // if they are all instances of the current window class, but not the current instance (proving that multiple windows are opened) if (itemType = thisType &&! Object. referenceEquals (item, this) {int itemPersonId = (int) itemType. getField ("personId "). getValue (item); // indicates that the record is edited. 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 void ChangeHandle () {if (MessageBox. show ("this data item has been modified in other windows and needs to be reloaded", "prompt", MessageBoxButtons. OK, MessageBoxIcon. information) = DialogResult. OK) {// reload the data Form2_Load (this, null );}}}
View Code

Test:

The following two editing windows are opened, and both of them edit the same data. When you edit one Name and save it, the other prompts that you need to refresh

 

 

Application. openForms; obtain all the currently opened windows, traverse and obtain their "Type" (Type, the same below) through reflection. If the "Type" is the same as the "Type" of the current window, when the current window is not used and the same data record is edited, the method is obtained and called by reflection to achieve the notification effect.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.