2014-10-16 73 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

相关问题