Updating datagridview only works inside functions with (object sender, EventArgs e) parameters

StackOverflow https://stackoverflow.com/questions/23607718

  •  20-07-2023
  •  | 
  •  

Question

I am writing this database using DataGridView, and created a second form in wich new data can be entered into my Local database tables. Everything works but the data is not updated in the datagrid view. So I have created another button_3 and in its functions called TableAdapter.Fill for both of my tables, and when this button is pressed the datagridview is updated. Now what i want is the DataGridView to be updated after i press the write button on form2 so that right after the form2 closes the new data is populated, but so far nothing is working.

Form1 :

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
    }



    public void button3_Click(object sender, EventArgs e)
    {  
        this.carsTableAdapter.Fill(this.databaseDataSet1.Cars);
        this.driversTableAdapter.Fill(this.databaseDataSet1.Drivers);       
    }

Form2

    private void Form2_Load(object sender, EventArgs e)
    {    
        this.carsTableAdapter.Fill(this.databaseDataSet1.Cars);    
        this.driversTableAdapter.Fill(this.databaseDataSet1.Drivers);
        this.carsBindingSource.AddNew();
        this.driversBindingSource.AddNew();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.carsBindingSource.EndEdit();
        this.driversBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.databaseDataSet1);
        this.Close();
        Form1 form1 = new Form1();
        form1.button3_Click(this, new EventArgs());
    }

So basicly my queston is how can i call the button3_Click event from form2 with those parameters, becouse calling the 2 TableAdapter.Fill lines in a function with no parameters does not work.

Était-ce utile?

La solution

Show Form2 as a modal dialog, which will prevent code after it from running until the form is closed.

Then move the logic from button3_Click to right after the form is displayed, like this:

private void button2_Click(object sender, EventArgs e)
{
    using (Form2 form2 = new Form2())
        form2.ShowDialog();

    this.carsTableAdapter.Fill(this.databaseDataSet1.Cars);
    this.driversTableAdapter.Fill(this.databaseDataSet1.Drivers);       
}

Remove the following code from button1_Click on the second form. It's not doing anything useful anyway, since you're creating a new instance of Form1 that has no relation to the current instance that's displayed to the user.

Form1 form1 = new Form1();
form1.button3_Click(this, new EventArgs());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top