2015-04-12 135 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? –