简体   繁体   English

从另一种形式刷新datagridview

[英]refresh datagridview from another form

i have two form. 我有两种形式。 main form and child form. 主要形式和子形式。 in main form show datagridview and in child form is a form to insert data to datagridview in main form.. so after i insert data from child form i want to refresh the datagridview in main form. 在主窗体中显示datagridview,在子窗体中是一种将数据插入主窗体中的datagridview的窗体..因此,从子窗体插入数据后,我想刷新主窗体中的datagridview。 so the new data appear in datagridview. 因此新数据将出现在datagridview中。 i tried this code, but the datagridview not refresh, i must close my application and reopen it to show the new 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();                        
    }

i also tried this, but not working too 我也试过了,但是也没用

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

You can add new event like this in frmNewBook 您可以在frmNewBook中添加这样的新事件

public event System.Action NotifyAnotherForm

Now put this event in under btnOpenForm_click at your main form 现在,将此事件放在主窗体的btnOpenForm_click下

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

At the main form, you must have a method to handle this event 在主窗体上,您必须具有处理此事件的方法

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

When you do some modification in frmNewBook, it will do an event in its proper place 当您在frmNewBook中进行一些修改时,它将在适当的位置进行事件

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

And if you more easy way, you can try do like this 如果您更简单的方法,可以尝试这样做

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

I advise you please put you parameter in SQL COMMAND to avoid Sql Injection 我建议您将参数放在SQL COMMAND中,以避免Sql Injection

also you can use "using". 您也可以使用“使用”。

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

            using(new frmChildForm())

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

        }
         loadDataGrid();  

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM