winform c Subform Close Refresh Instance of Parent Form

  • 2021-12-05 07:04:08
  • OfStack

Parent form Form1 child form Form2 Form1 has 1 datagridview control and 1 Add button,

There is an Text control and a Save button in Form2. It is required to click the Add button on the Form1 form to pop up Form2.

Enter the contents in text, click Save to automatically close Form2 and refresh the data in datagridview in Form1

In From1:


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

In From2:


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



Related articles: