2014-10-16 76 views
1

我有兩個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的粘貼或或多個選定的值。

有人可以幫助我。我提前感謝您的任何建議。 Image for reference

第一次嘗試。

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); 
} 

這有點帶來我想要的東西,但在錯誤的單元格加上循環不斷運行。 :( 我一直與第二環周圍擺弄但不能得到預期的結果.. :(

+0

看起來像一個嵌套for循環給我。看看迭代2D數組 - 應該可以幫助你:) – 2014-10-16 12:36:31

+0

我試過了,但我似乎沒有給出所需的結果。請給我看一個例子。 – 2014-10-16 12:43:27

+0

你能否給我看看你的嘗試,我們會看看我們能否指出你正確的方向 – 2014-10-16 12:44:39

回答

0

好吧所以我明白了...... 當你向dataGridView添加新行時,問題就會發生。它不會在現有數據之後添加,而是在其之前添加。因此會發生異常。 解決方案低於以防其他人需要它。 :)

int count =0; // declare in public class 

private void button3_Click(object sender, EventArgs e) 
    { 
     if (dataGridView2.SelectedRows.Count > 0) 
     { 
     dataGridView1.Rows.Add(dataGridView2.SelectedRows.Count); // add rows before entering data inside the new dataGridView. 
     } 
     foreach (DataGridViewRow row in dataGridView2.SelectedRows) 
     { 

      { 
       string value1 = row.Cells[0].Value.ToString(); 
       string value2 = row.Cells[1].Value.ToString(); 
       dataGridView1.Rows[count].Cells[0].Value = value1; 
       dataGridView1.Rows[count].Cells[1].Value = value2; 
      } 
     } 

     count =count + 1; 
     dataGridView2.ClearSelection(); // I used this because i have to work with 4 dataGridViews 
} 
1

像這將是對你有用:

private void dataGridView_SelectionChanged(object sender, EventArgs e) { 
      foreach (DataGridViewRow row in dataGridView.SelectedRows) { 
       string value1 = row.Cells[0].Value.ToString(); 
       string value2 = row.Cells[1].Value.ToString(); 
       //... 
       //also set the second datagrid from here too 
      } 
     } 

所以,在你的榜樣,你會想使用只是2樓和3單元格值,並將它們分配到其他的DataGrid :)


作爲一個側面說明,使您的變量更具有可讀性的/ etc,你應該習慣使用camelCase,

string strpartno應該成爲string strPartNo

以及來自您的評論命名dataGridViews正確的/ etc


,我意識到自己的INSERTINTO代碼需要稍微修改:

目前,您將它們放置在:

dataGridView1.Rows[0].Cells[1].Value = strppno; 
dataGridView1.Rows[0].Cells[2].Value = strpartno; 
       ^
        | 
       this will only place it into index 0 of the datagrid 

所以,我的建議是,首先

int count = ... //count number of rows in SecondDatagrid 

然後做:

dataGridView1.Rows[count++].Cells[1].Value = strppno; 
dataGridView1.Rows[count++].Cells[2].Value = strpartno; 
       ^
        | 
       this will only place it into next 
       available row of the datagrid    

這樣,你仍然插入前兩列,但不能覆蓋當前存儲的值。

+1

我實際上需要一個按鈕按下事件..我現在就試試這個......我一直使用camelCase。編輯讓我感到沮喪......所以最終的慣例開始改變。 :P:P:P 我會試試這個,讓你知道。 :) – 2014-10-16 13:42:35

+0

它給了我同樣的結果,我第一次嘗試。一旦我選擇並添加,它就會在dataGridView中進行。但是會發生什麼情況,我需要添加更多的行?我該如何實現 – 2014-10-16 13:54:59

+0

說,dataGridView2中總共有10行...我想將1,3,4,7添加到dataGridView1。我選擇1 ..點擊添加..這個工程。現在,當我選擇第3行並單擊添加時,應該在第1行下面的dataGridView1中添加新行,並且應該添加其他類似的行。目前它取代了第1行。 – 2014-10-16 15:54:39

相關問題