The following editor will bring you an example of closing a subform and refreshing the parent form in winform c#. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
The parent form Form1 and the child form Form2 have a datagridview control and an add button in Form1.
There is a Text control and a save button in Form2. Requirements Click the Add button on the Form1 form to pop up Form2, enter
in the text, click Save to automatically close Form2, and refresh the data in the datagridview in Form1
From1 Medium:
private void button3_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.ShowDialog(); if (f2.DialogResult == DialogResult.OK) { this.datagridBind();//重新绑定 } }
From2 Medium:
<BR><BR>private void button1_Click(object sender, EventArgs e) { string strConn = "server=.;database=filesSync;uid=sa;pwd=123"; SqlConnection conn = new SqlConnection(strConn); string sql = "insert into asyncFileList values ('" + textBox1.Text.ToString() + "')"; conn.Open(); SqlCommand myCmd = new SqlCommand(sql, conn); myCmd.ExecuteNonQuery(); conn.Close(); this.DialogResult = DialogResult.OK; MessageBox.Show("添加成功"); this.Close(); }
The above is the detailed content of Detailed explanation of the sample code for closing the subform and refreshing the parent form in winform c#. For more information, please follow other related articles on the PHP Chinese website!