将数据传递给DataGridViews

时间:2015-04-12 18:28:22

标签: c# winforms datagridview textbox

我的第二个表单上有文本框,下面是代码中的发送按钮。

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.PassName = richTextBox1.Text;
    f1.PassLastName = richTextBox2.Text;
    f1.PassAge = comboBox1.Text;
    f1.PassGender = richTextBox3.Text;
    f1.ShowDialog();
}
表单1上的

DataGridView以及此代码

public partial class Form1 : Form
{
    private string name;
    private string lastName;
    private string age;
    private string gender;

    public string PassName
    {
        get { return name; }
        set { name = value; }
    }

    public string PassLastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public string PassAge
    {
        get { return age; }
        set { age = value; }
    }

    public string PassGender
    {
        get { return gender; }
        set { gender = value; }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int n = dataGridView1.Rows.Add();
        dataGridView1.Rows[n].Cells[0].Value = name;
        dataGridView1.Rows[n].Cells[1].Value = lastName;
        dataGridView1.Rows[n].Cells[2].Value = age;
        dataGridView1.Rows[n].Cells[3].Value = gender;
    }

    private void mnuExit_Click(object sender, EventArgs e) //adding the quit on the top file with caution message
    {
        if (MessageBox.Show("Do you really want to Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            Application.Exit();
        }
    }

    private void addTask_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(); //show form2 so user can input data
        f2.ShowDialog();
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

}`

如果我想将一组数据发送到DataGridView,这很好,但是如果我再次添加新信息,则会打开一个新的DataGridView并将其存储到另一个单独的DataGridView然后我有两个DataGridView表格。我想将所有数据放在一个DataGridView上并继续添加行。因此,当用户点击带有DataGridView的第一个表单上的添加按钮时,它会打开表单2的TextBox表单,然后用户填写信息并单击发送按钮回到DataGridView的信息,然后会打开一个新窗口,其中包含新的DataGridView。我不希望这种情况发生,我希望它继续在第一个表单上添加行 有人能告诉我怎么做吗?

1 个答案:

答案 0 :(得分:1)

您可以使用 ShowDialog(this)所有者来获取父表单的属性。

<强> Form1中

private void Form1_Load(object sender, EventArgs e)
{
    //Move to Form1_Activated
    this.Activated += new System.EventHandler(this.Form1_Activated); //connect
}

private void Form1_Activated(object sender, EventArgs e)
{
    int n = dataGridView1.Rows.Add();
    dataGridView1.Rows[n].Cells[0].Value = name;
    dataGridView1.Rows[n].Cells[1].Value = lastName;
    dataGridView1.Rows[n].Cells[2].Value = age;
    dataGridView1.Rows[n].Cells[3].Value = gender;
}

private void addTask_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(); //show form2 so user can input data
    f2.ShowDialog(this);//set this form as Owner
}

<强>窗体2

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = (Form1)this.Owner;//Get the Owner form
    f1.PassName = richTextBox1.Text;
    f1.PassLastName = richTextBox2.Text;
    f1.PassAge = comboBox1.Text;
    f1.PassGender = richTextBox3.Text;
    //f1.ShowDialog();
    f1.Show();
    this.Close();
}