我有兩個datagridviews在一個形式有一些類似的colums 當用戶點擊一個按鈕時,第二個datagridview的第二列和第三列selectedrow的數據應該去第1和第一個datagridview的第二列。 我檢索使用從一個datagridview到另一個c#的數據.net
string strppno = dataGridView2.SelectedCells[1].Value.ToString();
string strpartno = dataGridView2.SelectedCells[2].Value.ToString();
的數據,我可以使用
dataGridView1.Rows[0].Cells[1].Value = strppno;
dataGridView1.Rows[0].Cells[2].Value = strpartno;
我怎麼過無法弄清楚如何循環,以便它可以多次使用第二datagridview的粘貼或或多個選定的值。
有人可以幫助我。我提前感謝您的任何建議。
第一次嘗試。
for (int index = 0; index < dataGridView2.Rows.Count; index++)
{
int editIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[editIndex].Cells[0].Value = dataGridView2.Rows[index].Cells[1].Value;
dataGridView1.Rows[editIndex].Cells[1].Value = dataGridView2.Rows[index].Cells[2].Value;
}
這將帶出datagridview中的所有內容2.我只想選中一個。
第二次嘗試。
string strppno = dataGridView2.SelectedCells[1].Value.ToString();
string strpartno = dataGridView2.SelectedCells[2].Value.ToString();
for (int i = dataGridView2.SelectedRows.Count; i < dataGridView2.SelectedRows.Count + 1; i++)
{
dataGridView1.Rows[i].Cells[i].Value = strppno;
dataGridView1.Rows[i].Cells[i + 1].Value = strpartno;
}
dataGridView1.Rows.Add(dataGridView2.SelectedRows.Count);
}
這有點帶來我想要的東西,但在錯誤的單元格加上循環不斷運行。 :( 我一直與第二環周圍擺弄但不能得到預期的結果.. :(
看起來像一個嵌套for循環給我。看看迭代2D數組 - 應該可以幫助你:) – 2014-10-16 12:36:31
我試過了,但我似乎沒有給出所需的結果。請給我看一個例子。 – 2014-10-16 12:43:27
你能否給我看看你的嘗試,我們會看看我們能否指出你正確的方向 – 2014-10-16 12:44:39