datagridview in form1 automatic refresh after insert from form2

I have small issue, I have datagridview in form1 to which I insert data from form2 using SQL commands, but after button click which includes INSERT command the newly inserted value doesn't appear in form1's datagridview. Is there any way to solve this ?

So I had to make refresh button in form1 so I can refresh it every time something is inserted.

Thanks in advance.

This is the insert code in button click in form2: private void btn_zaj_uloz_Click(object sender, EventArgs e) {

        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();



    }

Some thoughs on the code. Unless I'm missing something, the snippet does the insert (that's OK) and then reads all the data back, but does nothing with the result, so Form1 in fact never realizes that something happened at all.

What you need to do is some how notify Form1 that Form2 has finished with its insert, and pass back the data. The grid should have some sort of data source from where it takes the rows to display, and you need to add the newly created data from Form2 there. The best way I can think of is to create an event on Form2 that Form1 will suscribe to, passing all the data needed to recreate the record.

First create the event handler data:

public class InsertCompleteEventArgs : EventArgs
{
    public string zakce {get;set;}
    public string zname {get;set;}
    /*other fields go here*/
}

In Form2, declare the event:

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});
    }
}

In your code, replace the extra "SELECT * FROM zajezd", raise the event instead:

This.OnInsertComplete(zakce.Text,zname.Text);

So, with that, Form2 will raise an event every time a new record is inserted, so anyone interested can tell when to update itself. All that rest to do is to suscribe to that event from Form1 when launching Form2:

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 */
}
链接地址: http://www.djcxy.com/p/71160.html

上一篇: 从另一个窗体访问字符串

下一篇: form2中的datagridview在从form2插入后自动刷新