1

我有两种形式。主窗体和子窗体。在主窗体中显示 datagridview,在子窗体中是一种将数据插入主窗体中的 datagridview 的窗体。所以在我从子窗体插入数据后,我想在主窗体中刷新 datagridview。所以新数据出现在datagridview中。我试过这段代码,但datagridview没有刷新,我必须关闭我的应用程序并重新打开它以显示新的datagridview ...

public void button1_Click(object sender, EventArgs e)
    {
        string cstr = "server=localhost;User Id=root;database=sma9";
        con1 = new MySqlConnection(cstr);
        con1.Open();
        com1 = new MySqlCommand();
        com1.Connection = con1;
        com1.CommandType = CommandType.Text;
        com1.CommandText = "INSERT INTO tbukux (kodebuku,judulbuku,namakategori,pengarang,penerbit,tahunterbit,stokbuku) VALUES ('" + txtkode.Text + "','" + txtjudul.Text + "','" + txtkategori.Text + "','" + txtpengarang.Text + "','" + txtpenerbit.Text + "','" + txttahun.Text + "','" + txtstok.Text + "')";
        com1.ExecuteNonQuery();            
        con1.Close();
        Form1 form1 = new Form1();
        form1.gridbuku.RefreshEdit();                        
    }

我也试过这个,但也没有工作

public void button1_Click(object sender, EventArgs e)
    {
        Form1 form1 = new Form1();
        string cstr = "server=localhost;User Id=root;database=sma9";
        con1 = new MySqlConnection(cstr);
        con1.Open();
        com1 = new MySqlCommand();
        com1.Connection = con1;
        com1.CommandType = CommandType.Text;
        com1.CommandText = "INSERT INTO tbukux (kodebuku,judulbuku,namakategori,pengarang,penerbit,tahunterbit,stokbuku) VALUES ('" + txtkode.Text + "','" + txtjudul.Text + "','" + txtkategori.Text + "','" + txtpengarang.Text + "','" + txtpenerbit.Text + "','" + txttahun.Text + "','" + txtstok.Text + "')";
        com1.ExecuteNonQuery();
        com2 = new MySqlCommand();
        com2.Connection = con1;
        com2.CommandType = CommandType.Text;
        com2.CommandText = "select * from tbukux";
        ds1 = new DataSet();
        adp1 = new MySqlDataAdapter(com2);
        adp1.Fill(ds1, "tbukux");
        form1.gridbuku.DataSource = ds1;
        form1.gridbuku.DataMember = "tbukux";
        con1.Close();            
        form1.gridbuku.Refresh();                        
    }
4

2 回答 2

1

您可以在 frmNewBook 中添加这样的新事件

public event System.Action NotifyAnotherForm

现在将此事件放在主窗体的 btnOpenForm_click 下

frmNewBook form = new frmNewBook();
form.NotifyAnotherForm += new System.Action(mainForm_NotifyAnotherForm);
form.Show(); // or .ShowDialog();

在主窗体,你必须有一个方法来处理这个事件

public void mainForm_NotifyAnotherForm() {
    //put you code for update datagrid
}

当您在 frmNewBook 中进行一些修改时,它会在适当的位置执行一个事件

if (NotifyAnotherForm != null) {
   NotifyAnotherForm();
}

如果你更简单的方法,你可以尝试这样做

frmNewBook form = new frmNewBook();

//You code will pause until form.Show close
form.Show(); // or .ShowDialog();

//After frmNewBook close, code will continue running and you can put code to update
loadDataGrid();

我建议你把你的参数放在 SQL COMMAND 中以避免Sql Injection

于 2012-11-16T14:15:01.473 回答
0

您也可以使用“使用”。

      public void button1_Click(object sender, EventArgs e)
    {
       frmChildForm ofrmchild = new frmChildForm();

            using(new frmChildForm())

        {
            ofrmchild .BringToFront();
            ofrmchild .ShowDialog();

        }
         loadDataGrid();  

   }
于 2014-01-23T14:06:29.090 回答