【问题标题】:refresh datagridview from another form从另一个表单刷新 datagridview
【发布时间】:2012-05-25 15:46:43
【问题描述】:

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

【问题讨论】:

  • 尝试 form1.gridbuku.DataSource = null ,然后每次需要刷新网格时重新分配数据源。

标签: c# forms datagridview refresh


【解决方案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

【讨论】:

    【解决方案2】:

    你也可以使用“使用”。

          public void button1_Click(object sender, EventArgs e)
        {
           frmChildForm ofrmchild = new frmChildForm();
    
                using(new frmChildForm())
    
            {
                ofrmchild .BringToFront();
                ofrmchild .ShowDialog();
    
            }
             loadDataGrid();  
    
       }
    

    【讨论】:

      猜你喜欢
      • 2015-01-16
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多