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