C# form1中的datagridview从form2插入后自动刷新
我有一个小问题,我在form1中有datagridview,我使用SQL命令将form2中的数据插入其中,但在单击包含insert命令的按钮后,新插入的值不会出现在form1的datagridview中。有什么办法解决这个问题吗 所以我必须在form1中创建“刷新”按钮,以便每次插入内容时都能刷新它 提前谢谢 这是表单2中单击按钮时插入代码: 私有void btn_zaj_uloz_单击(对象发送方,事件参数e) {C# form1中的datagridview从form2插入后自动刷新,c#,sql,datagridview,insert,C#,Sql,Datagridview,Insert,我有一个小问题,我在form1中有datagridview,我使用SQL命令将form2中的数据插入其中,但在单击包含insert命令的按钮后,新插入的值不会出现在form1的datagridview中。有什么办法解决这个问题吗 所以我必须在form1中创建“刷新”按钮,以便每次插入内容时都能刷新它 提前谢谢 这是表单2中单击按钮时插入代码: 私有void btn_zaj_uloz_单击(对象发送方,事件参数e) { SqlCommand prikaz = new SqlComm
SqlCommand prikaz = new SqlCommand
("INSERT INTO zajezd(akce,name,zeme,hotel,h_adresa,odjdate,pridate,pocdnu,pocnoc,klimax)values(@zakce,@zname,@zzeme,@zhotel,@zh_adresa,@zodjdate,@zpridate,@zpocdnu,@zpocnoc,@zklimax)", spojeni);
prikaz.Parameters.AddWithValue("zakce", zakce.Text);
prikaz.Parameters.AddWithValue("zname", zname.Text);
prikaz.Parameters.AddWithValue("zzeme", zzeme.Text);
prikaz.Parameters.AddWithValue("zhotel", zhotel.Text);
prikaz.Parameters.AddWithValue("zh_adresa", zh_adresa.Text);
prikaz.Parameters.AddWithValue("zodjdate", zodjdate.Text);
prikaz.Parameters.AddWithValue("zpridate", zpridate.Text);
prikaz.Parameters.AddWithValue("zpocdnu", zpocdnu.Text);
prikaz.Parameters.AddWithValue("zpocnoc", zpocnoc.Text);
prikaz.Parameters.AddWithValue("zklimax", zklimax.Text);
spojeni.Open();
prikaz.ExecuteNonQuery();
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.SqlClient.SqlDataAdapter SDA = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM zajezd", spojeni);
SDA.Fill(dt);
spojeni.Close();
this.Close();
}
对代码有一些想法。除非我遗漏了什么,否则代码段会进行插入(没关系),然后读取所有数据,但对结果没有任何影响,因此Form1实际上从未意识到发生了什么 您需要做的是如何通知Form1 Form2已完成插入,并传回数据。网格应该有某种数据源,从中可以显示行,您需要将Form2中新创建的数据添加到其中。 我能想到的最好的方法是在Form2上创建一个Form1将使用的事件,传递重新创建记录所需的所有数据 首先创建事件处理程序数据:
public class InsertCompleteEventArgs : EventArgs
{
public string zakce {get;set;}
public string zname {get;set;}
/*other fields go here*/
}
在表格2中,声明事件:
public event EventHandler<InsertCompleteEventArgs> InsertComplete;
protected void OnInsertComplete(string zakce, string zname /*other data*/)
{
EventHandler<InsertCompleteEventArgs> handler = this.InsertComplete;
if(handler!=null)
{
handler(this,new InsertCompleteEventArgs(){zakce=zakce,zname=zname});
}
}
因此,每次插入新记录时,Form2都会引发一个事件,因此任何感兴趣的人都可以知道何时更新自己。剩下要做的就是在启动Form2时从Form1中引用该事件:
public Button_Click(object sender, EventArgs e)
{
/* This will be your existing code when you show Form2 */
Form2 form=new Form2();
form.Insertcomplete += this.Form2_InsertComplete; //this is where the notification is requested
form.Show();
}
public Form2_InsertComplete(object sender, InsertCompleteEventArgs e)
{
/* From here you add the new record to the existing DataSource of the DataGridView using the properties of the "e" object you receive */
}
public Button_Click(object sender, EventArgs e)
{
/* This will be your existing code when you show Form2 */
Form2 form=new Form2();
form.Insertcomplete += this.Form2_InsertComplete; //this is where the notification is requested
form.Show();
}
public Form2_InsertComplete(object sender, InsertCompleteEventArgs e)
{
/* From here you add the new record to the existing DataSource of the DataGridView using the properties of the "e" object you receive */
}