C # uses Action delegate method to solve the problem of cross-page information refresh

For the usage of Action delegation, please refer to the blog post:
https://blog.csdn.net/qq_39217004/article/details/105465485
In a project example, the following problem needs to be solved:
Click "Edit" on the student list interface to pop up student information In the editing interface, click the "modify" button on the editing interface to successfully modify the student's information and transfer it to the database. At the same time, the information in the datagridview control in the StudentList interface is refreshed into the modified information. Involved in the problem of cross-page refresh, I use the Action delegate method to solve
Insert picture description here

The idea is as follows:
First, the function LoadAllStudentList () on the FrmStudentList interface to load student information. The function of this function is to read information from the database and store it in the datatable table, and then display it in the datagridview of the interface.
To solve the problem of cross-page information refresh, I use the following method: In
layman's terms, the method of loading information is assigned to the delegate on the list page, and the student number (to be changed) and the delegate are passed to the Edit interface. After the Edit interface receives the two pieces of information, after the modification is successful, the method of the list page is called again, that is, a delegate is called.
Insert picture description here

1. Define the delegate on the StudentList page, assign the method of loading the data list to the delegate, and pass it to the modification page at the same time

private Action reLoad =null;//在StudentList页面定义委托
reLoad = LoadAllStudentList;//把加载学生列表的方法赋值给委托reLoad
//封装了一个TagObject类,用委托的方法传值,传学号和委托到修改页面
FrmEditStudent frmEditStudent = new FrmEditStudent();
  frmEditStudent.Tag = new TagObject()
  {
     StuId = stuId,
     ReLoad = reLoad
 };
 public class TagObject
{
     public int StuId { get; set; }
     public Action ReLoad { get; set; }
}

2. Modify the page definition delegation, assign the passed delegation to the delegation defined on this page, and call the delegation after the modification is successful

private Action reLoad =null;//在Edit页面定义委托
//修改页面定义委托,把传过来的委托赋值给本页面定义的委托
 TagObject tagObject = (TagObject)this.Tag;
 stuId = tagObject.StuId;
 reLoad = tagObject.ReLoad;
//提示修改成功之后,调用委托
MessageBox.Show($"学生:{stuName}信息修改成功", "修改学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
reLoad(); //调用

After modifying the information, the interface
Insert picture description here

Published 18 original articles · praised 0 · visits 233

Guess you like

Origin blog.csdn.net/qq_39217004/article/details/105467585