0
votes

I'm trying for several hours to figure it out how can I refresh a previous form (Form1) when I save data on Form2.

I'm using btnAdd_Click on Form1 in order to open me Form2 where user can enter data, so far so good. What I want is: when the user click on save button, the data he saved (inserted into a database) to be visible in the previous form (Form1) datagridview, which is not happening. I'm using the code below to insert data->

        private void btnAdauga_Click(object sender, EventArgs e)
    {

       string cs = "Data Source=IS020209;Initial Catalog=TrafficManager;Integrated Security=True";
       string insert = "INSERT INTO BILETE (IDPasager, IDCursa, Codbagaj, DataAchizitie, Pret) VALUES (@IDPasager, @IDCursa, @Codbagaj,'"+this.dataBilet.Text+ "', @Pret)"; 


       try
       {
           if (txtCod.Text.Trim() == "" && grdPasageri.SelectedRows.Count==0 && grdCurse2.SelectedRows.Count==0) { MessageBox.Show("Trebuie sa introduceti codul bagajului si sa selectati un pasager pentru o cursa!"); }
           else 
           {
               using (SqlConnection con = new SqlConnection (cs))
               {
                   con.Open();
                   SqlCommand cmd = new SqlCommand(insert, con);
                   cmd.Parameters.Clear();
                   cmd.Parameters.AddWithValue("IDPasager", int.Parse(grdPasageri.CurrentRow.Cells[0].FormattedValue.ToString().Trim()));
                   cmd.Parameters.AddWithValue("IDCursa", int.Parse(grdCurse2.CurrentRow.Cells[0].FormattedValue.ToString()));
                   cmd.Parameters.AddWithValue("Codbagaj", int.Parse(txtCod.Text.Trim()));
                   cmd.Parameters.AddWithValue("Pret", txtPret.Text);
                   int val = cmd.ExecuteNonQuery();
                   MessageBox.Show( val + "Biletul a fost adaugat cu succes pentru cursa selectata!");
                   con.Close();
                   this.Dispose();

               }
           }



       }
       catch (Exception er) { MessageBox.Show(er.Message); }
    }

I've been trying using this.Refresh() but didn't work. Please provide some tips. Thanks!

3

3 Answers

0
votes

Pass the instance of Form1 to the constructor of Form2 when creating Form2 (e.g. Form2 form2 = new Form2(this);) and store it inside a member of Form2 inside its constructor (e.g. as member myForm1 with myForm1 = form1; where form1 is the argument of the constructor). Then just call myForm1.Refresh();.

0
votes

There are many ways of keeping tracking of other form, you can pass the parent form as parameter to child form's constructor, you can have a property for parent form etc.

You can also get already opened form using Application.OpenForms collection.

In your case you can do something like:

Form1 form1 = Application.OpenForms["Form1"] as Form1;
if(form1 != null) // found
{
   form1.RefreshMethod(); //call your public method from Form1 to refresh
}

IMO, a better approach would be to pass the parent form as parameter to child's constructor, but I have seen and used Application.OpenForms collection as well.

As a side note, irrelevant to your current question, use parameters for @Codbagaj,'"+this.dataBilet.Text+ " as well , there is no need to do string concatenation, it will be consistent to the good practice of using parameters instead of string concatenation.

0
votes

The easiest way to do this, is set "Form1" as the owner of the form that you are opening (the "AddData" form) and then call it from the opened form once the user has closed it/pressed a button. See example.

//Form1
public void RefreshWork()
{
// Do Something Here
}

btnAdd_Click(object sender, EventArgs e)
{
AddData TempForm = new AddData();
TempForm.Owner = this;
TempForm.ShowDialog();
}

// "AddData" form

AddData_FormClosing(object sender, EventArgs e)
{
((Form1)Owner).RefreshWork();
}