使用按钮将选定的数据从datagrid复制到另一个 [英] copy selected data from datagrid to another with button

查看:69
本文介绍了使用按钮将选定的数据从datagrid复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我有一个表格有两个datagrids.i想要一个按钮来复制选定的行

来自一个接一个。

我找到这个代码:

Hi guys!

I have a form which has two datagrids.i want with a button to copy the selected rows
from one to another.
I find this code :

private void button1_Click(object sender, EventArgs e)
       {

           foreach (DataGridViewRow row in this.table1DataGridView.SelectedRows)
           {
               object[] rowData = new object[row.Cells.Count];
               for (int i = 0; i < rowData.Length; ++i)
               {
                   rowData[i] = row.Cells[i].Value;
               }
               this.copyDataGridView.Rows.Add(rowData);
           }
       }



但它会抛出异常:



其他信息:当控件是数据绑定时,无法以编程方式将行添加到DataGridView的行集合中。


but it throw an exception:

Additional information: Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

推荐答案



我创建了一个你的样本非常简单,但工作正常:

Hi,
I've created a sample for you which is very simple but works fine:
private void button1_Click(object sender, EventArgs e)
      {
          List<object> destList= new List<object>();
          foreach (DataGridViewRow row in dataGridView1.SelectedRows)
              destList.Add(row.DataBoundItem);
          dataGridView2.DataSource = destList;
      } 





dataGridView1的数据源是一个数据集,它是数据集。



干杯。



dataGridView1's datasource is a dataset which is databound.

Cheers.




从异常消息中可以看出,在目标DataGrid中你需要清除数据绑定标签,因为你想要添加行编程。



干杯
Hi,
As from the Exception message is clear, in the destination DataGrid you need to clear databinding tags as you want to add rows programmatically.

Cheers


这篇关于使用按钮将选定的数据从datagrid复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆