我有兩種形式。主要形式和兒童形式。在主窗體中顯示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();
}
嘗試form1.gridbuku.DataSource = null,並且每次需要刷新網格時再次分配數據源。 – Sadaf
@husnain,不工作sir :( –