0

Suppose we have two forms. form1 called form2 using ShowDialog() function. What is the best way to make a code run in form1 only after form2 is closed? Actually I want to fill a data grid view in form1 using data from form2.

void frmAnalysis_Activated(object sender, EventArgs e)
    {
        //I am using this event to add rows
        if (selectedEXP.Count != 0)
        {
            dgvExperiments.Rows.Clear();
            foreach (SelectedExperiments s in selectedEXP)
            {
                for (int i = 0; i < s.size; i++)
                {
                    int index = dgvExperiments.Rows.Add();
                    dgvExperiments.Rows[index].Cells["Experiment"].Value = s.name;
                }
            }
        }
    }




#region Update database
//and here is a code to update data but the problem that the 
// foreach loop is skipped since there is no rows in dgvExperiments
// however I can see a row when the form is Active
        if(dgvExperiments.Rows.Count >0)
        {
            MessageBox.Show("True");
        }
        try
        { int k = 0;
            OleDbDataAdapter da;
            da = new OleDbDataAdapter("select * from [AnalysisExperiments]", conn);
            string ExpQuery = "update AnalysisExperiments set SampleNumber = @SampleNumber, Status = @Status where ID = '" + tbJobNumber.Text + "' and Experiment = '";

            foreach (DataGridViewRow row in dgvExperiments.Rows)
            {
                ExpQuery = ExpQuery + row.Cells["Experiment"].Value.ToString() + "'";
                OleDbCommand updateCommand = new OleDbCommand(ExpQuery, conn);
                updateCommand.Parameters.Add("@SampleNumber", OleDbType.VarWChar);                    
                updateCommand.Parameters["@SampleNumber"].Value = row.Cells["SampleNumber"].Value.ToString();
                updateCommand.Parameters.Add("@Status", OleDbType.Boolean);
                updateCommand.Parameters["@Status"].Value = row.Cells["Status"].Value;
                da.UpdateCommand = updateCommand;

                conn.Open();
                k = da.UpdateCommand.ExecuteNonQuery();
                conn.Close();
            }

            if (k == 1)
                MessageBox.Show("Done");
            else
            {
                MessageBox.Show("Nothing Updated!");
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        #endregion

I am trying to update some data but I don't what is wrong with the rows of dgvExperiments.

  • Possible duplicate of [Send values from one form to another form](http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – JohnG Feb 28 '17 at 10:24
  • I left an answer, replace `SomeFunction` with a function that has data as parameter(s) and fills the datagridview. And replace when its called. – EpicKip Feb 28 '17 at 10:33
  • Form1 will wait for form2 if you call ShowDialog(). Otherwise you can setup your own event which gets fired when form2 is closing. – Philipp Müller Feb 28 '17 at 10:39
  • @MohammedAl-A'mri If you are having trouble filling the datagridview, post that code here so I can have a look at it. – EpicKip Feb 28 '17 at 11:05
  • It is a large one ): but I will post it @EpicKip – Mohammed Al-A'mri Feb 28 '17 at 11:39
  • On what form is dvgExperiments? And also in what form is the code you posted? I can make an example with a datagridview if you want? – EpicKip Feb 28 '17 at 12:08
  • both in one form called frmAnalysis. It will be good to give example. I noticed that dvgExperiments columns are cleared before update I don't know where exactly! @EpicKip – Mohammed Al-A'mri Feb 28 '17 at 12:14
  • If you want to send anything from `form2` to `form1` (question title) just make a `List` of the data you want to send and make a method in `form1` that takes the `List` as parameter. – EpicKip Feb 28 '17 at 12:18
  • I made a small edit where I update a dataGridView on Form1 with data from Form2 – EpicKip Feb 28 '17 at 12:19

2 Answers2

2

This is an example to illustrate how to do it, replace the data and functions with your actual data and functions.

Use a code like so:

Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

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

    }

    public void SomeFunction(string someData)
    {
        dataGridView1.Rows.Clear();
        foreach(string data in someData)
        {
            dataGridView1.Rows.Add(data);
        }
    }
}

As you see here I use this as parameter for new Form2
Form2:

public partial class Form2 : Form
{
    Form1 MainForm;
    public Form2(Form1 form)//This is why you need to give "this" as parameter
    {
        InitializeComponent();
        MainForm = form;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Here you call your function and send the data to fill the DataGridView
        List<string> listOfData = new List<string> {"someDataA", "someDataB"};
        MainForm.SomeFunction(listOfData);
        this.Close();
    }
}

As you see the parameter is here: public Form2(Form1 form) there we make it accessible in the class MainForm = form and then after a certain action (button click in my case) we call the SomeFunction from the mainform and pass data along.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
  • Don't see the reason why `Form2 form2 = new Form2(this);` needs to have `this` or how it benefits him, but upvoted anyway, maybe you could elaborate on this? – Joel Feb 28 '17 at 10:38
  • `this` means `Form1` in this example so I sent Form1 to form2 so I can access its methods from form2 without making a new instance (if you make a new instance it wont work for updating UI because you want to alter the existing instance) – EpicKip Feb 28 '17 at 10:40
  • @Joel To be exact `this` is a reference to the instance of the class its called from. I update my answer a bit maybe that will clear it up :) – EpicKip Feb 28 '17 at 10:42
  • it run the function right but grid view rows do not appear – Mohammed Al-A'mri Feb 28 '17 at 10:55
  • Show your code for the datagridview rows... I can't tell whats wrong without it – EpicKip Feb 28 '17 at 10:59
  • If you just do `dataGridView.Rows.Add("data col1", "data col2");` wrapped in a `foreach` populated with your data it should work. Don't forget `dataGridView.Rows.Clear();` if you want to "refresh" your data. – EpicKip Feb 28 '17 at 11:02
0

If you are using ShowDialog() the execution of code in Form1 will "stop" till Form2 is closed. That's what you want I guess. If you have e.g. a public property in Form2 you can access the data from Form1 after Form2 was closed. Have a look at the links JohnG provided for examples.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51