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.