2013-02-26 168 views
-4

我的項目中有兩種形式。在form1中,我有一個dataGridView,並在form2中有4個TextBoxes。我想要從一個DataGridview中使用CellMouseClick事件的變量中獲得一個值,然後將它傳遞給Form2中的一個TextBox將數據從一個表單傳遞到另一個表單

我試過這個。

Form1上#它給我一個錯誤

public form(int id) 
{ 
    int x; 
    x = dataGridView1.CurrentRow.Cells[0].Value.ToString(); 
} 

什麼亞姆想在窗口2

回答

7

做一個constructor可以constructconstruction給定的前提條件類型。

如果這意味着一個整數,那麼就這樣吧:

public MyForm(int id) { 
    SomeIdProperty = id; 
} 

代替var form = new MyForm();,做到:

var form = new MyForm(idOfTheRelevantThing); 

然後表現出來。

+0

,我該如何稱呼它從另一種形式 – 2013-02-26 22:25:32

+0

比它工作完美 – 2013-02-27 02:21:18

2

如果從Form1顯示Form2,則可以使用構造函數傳遞該值。事情是這樣的:

class Form2 { 
    public string Value { get; set; } 
    public Form2(string value) { 
     Value = value; 
    } 

    public void Form2_Load() { 
     textBox1.Text = Value; 
    } 
} 

,並做到這一點(內Form1.cs):

Form2 f = new Form2("the value here"); 
f.ShowDialog(); //or f.Show(); 
4

Form1中

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     var frm2 = new Form2(dataGridView1.Rows[0].Cells[0].Value.ToString()); 
     frm2.Show(); 
    } 
} 

窗體2

public partial class Form2 : Form 
{ 
    public Form2(string s) 
    { 
     InitializeComponent(); 
     textBox1.Text = s; 
    } 
} 
相關問題