2012-05-18 82 views
1

我有兩種形式。主要形式和兒童形式。在主窗體中顯示datagridview和子窗體是一種形式插入數據到主窗體中的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();       
    } 
+0

嘗試form1.gridbuku.DataSource = null,並且每次需要刷新網格時再次分配數據源。 – Sadaf

+0

@husnain,不工​​作sir :( –

回答

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

0

也可以使用「using」。

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

      using(new frmChildForm()) 

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

     } 
     loadDataGrid(); 

    }