使用ShowDialog()连接表单 [英] Connected Forms with ShowDialog()

查看:54
本文介绍了使用ShowDialog()连接表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两种形式. form1使用ShowDialog()函数称为form2.仅在form2关闭后才能使代码在form1中运行的最佳方法是什么? 其实我想使用form2中的数据填充form1中的数据网格视图.

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

我正在尝试更新一些数据,但是dgvExperiments的行没有问题.

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

推荐答案

这是一个说明如何执行此操作,将数据和函数替换为实际数据和函数的示例.

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

使用如下代码:

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

正如您在此处看到的,我将this用作new Form2
的参数 Form2:

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

如您所见,参数在这里:public Form2(Form1 form)在那里我们可以在类MainForm = form中对其进行访问,然后在执行某些操作(在我的情况下单击按钮)之后,我们从主窗体调用SomeFunction并传递数据一起.

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.

这篇关于使用ShowDialog()连接表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆