从另一个表单刷新datagridview

时间:2012-05-18 03:31:28

标签: c# forms datagridview refresh

我有两种形式。主要形式和儿童形式。在主窗体中显示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();                        
    }

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你也可以使用“使用”。

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

            using(new frmChildForm())

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

        }
         loadDataGrid();  

   }