2015-04-12 105 views
0

我在第二个表单上有一个文本框,并在下面显示的代码中有一个发送按钮。将数据传递给DataGridViews

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上并继续添加行。因此,当用户单击第一个表单上的添加按钮时,它会打开TextBox表单,它是表单2,然后用户填写信息并单击发送按钮,将信息发送回DataGridView,但是,此操作会将此信息发送回DataGridView然后用新的DataGridView打开一个新窗口。我不希望发生这种情况,我希望它继续在第一个表单上添加行。
有人可以告诉我如何做到这一点?

回答

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

这看起来很有希望,但是现在,当我上发送按钮没有文本框的单击/组合框字段显示在DataGridView。有任何想法吗? – Sup

+0

哪个按钮是发送按钮? Form2中的button1? –

+0

您是否使用Form1连接了Form1_Activated? –