2016-01-13 59 views
-1

我想要做的是當用戶雙擊一行時會彈出一個單獨的表單。然後使用DataGridView中的值填充新表單中的TextBoxes。我可以讓表單從雙擊出現,之後我不知道該怎麼辦? 我的代碼:從一個DataGridView傳遞值到一個單獨的表格

private void dgvTable_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) 
{ 
    FrmInfo frmInfo = new FrmInfo(); 

    frmInfo.ShowDialog(); 
    frmInfo.Dispose(); 
    var notes = dgvTable.Rows[e.RowIndex].Cells["Notes"].Value; 
} 

感謝

+0

我不知道你到目前爲止有什麼代碼,但你不能創建一個新的窗體的構造函數,它將dataGridView中的所需數據作爲參數,並在雙擊事件中使用該構造函數創建窗體? – spersson

+3

[將值從一個窗體發送到另一個窗體]可能的重複(http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – stuartd

回答

1

很容易! 首先你不能達到其他形式在C#在vb.net不正常的方式來控制,這樣你就可以做到以下幾點:

  • 準備形式接收數據:

1 - 在窗體構造函數中void add參數來處理接收到的數據。

2-在InitializeComponent();之後;空添加的代碼行,象下面這樣:

public frm1(string txt1, string txt2, string txt3) 
{ 
    InitializeComponent(); 
    textBox1.Text = txt1; 
    textBox2.Text = txt2; 
    textBox3.Text = txt3; 
} 
  • 處理DataGridView的dg_RowHeaderMouseDoubleClick事件:

    - 注意:您還可以處理CellDoubleClick事件在你的榜樣,但我的事件是使更有意義。

1-創建變量來保存發送的數據。

2-爲將處理數據的表單創建一個新的表單實例,並按照確切的順序將行單元格數據傳遞給方法參數。

private void dg_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    //Collect the row cells values: 
    string val1 = dg.Rows[e.RowIndex].Cells[0].Value.ToString() //data of the first cell in the row 
    string val2 = dg.Rows[e.RowIndex].Cells[1].Value.ToString() //data of the second cell in the row 
    string val3 = dg.Rows[e.RowIndex].Cells[2].Value.ToString() //data of the third cell in the row 
    //Initialize a new instance of the data handle form and send the row data to it: 
    var newFrm = new frm1(val1, val2, val3); 
    newFrm.Show();     
} 

依此類推,您可以創建儘可能多的參數。

+0

Thanks,put im stuck on你的第一步?你可以舉一個例子 – AndroidAL

+0

@AndroidAL在你的例子中將會有文本框-frmInfo的表單中查看它的代碼,你應該在頂部看到一個void: 'frmInfo()'這是表單構造函數,修改它就像答案:'公共frmInfo(字符串txt1,字符串txt2,字符串txt3)' 嘗試並回復... –

相關問題