父窗体Form1 子窗体Form2

Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮

要求点击Form1窗体上的添加按钮,弹出Form2,再text里面输入内容,点击保存自动关闭Form2,刷新Form1中datagridview中的数据

From1中:

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

From2中:

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();
            
        }

你可能感兴趣的:(父窗体Form1 子窗体Form2)