English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Example of Closing and Refreshing Parent Form in winform C# Subform

parent window Form1 child window Form2 Form1There is a datagridview control and an add button,

Form2with a Text control and a save button, requiring to click Form1the add button on the form, pop up Form2,

enter content in text, click save to automatically close Form2, refresh Form1in datagridview data

From1in: 

private void button3_Click(object sender, EventArgs e) 
    { 
      Form2 f2 = new Form2(); 
      f2.ShowDialog(); 
      if (f2.DialogResult == DialogResult.OK) 
      { 
        this.datagridBind();//Rebind 
      } 
    } 

From2in:

<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("Added successfully"); 
      this.Close(); 
    } 

This is the full content of the example of closing and refreshing the parent form in winform C# subform that the editor shares with everyone. I hope it can provide a reference for everyone, and I also hope everyone will support the呐喊 tutorial more.

You May Also Like