C#Datagridview行移动另一个datagridview [英] C# Datagridview rows move another datagridview

查看:99
本文介绍了C#Datagridview行移动另一个datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好先生,



我有两个Datagridviews从数据库中获取结果(动态)。两个Datagridview都有相同的no列,第一列为复选框:



Hello Sir,

I have two Datagridviews which get results from database (dynamically). both Datagridview have equal no of columns with first column as checkbox:

DataGridViewCheckBoxColumn CBColumn2 = new DataGridViewCheckBoxColumn();
CBColumn2.HeaderText = "Confirm";
CBColumn2.FalseValue = false;
CBColumn2.TrueValue = true;
dataGridView2.Columns.Insert(0, CBColumn2);





我想将选定的单行从Datagridview 2移动到Datagridview 1.我使用了以下代码段。它正在努力删除所选数据,但它没有移动并从VS2008获取以下错误消息当控件受数据限制时,无法以编程方式将行添加到DataGridView的行集合中。



这是代码段,请帮帮我。谢谢!





I want to move selected single row from Datagridview 2 to Datagridview 1. I have used following code segement. It's working to remove the selected data, but it's not moving and getting following error message from VS2008 "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

Here is the code segment, please help me. Thank You!

foreach (DataGridViewRow selRow in dataGridView2.SelectedRows.OfType<DataGridViewRow>().ToArray())
            {
                dataGridView2.Rows.Remove(selRow);

                dataGridView1.Rows.Add(dataGridView2.SelectedRows);


            }

推荐答案

错误信息非常明确:

当控件受数据绑定时,无法以编程方式将行添加到DataGridView的行集合中。



所有它说的是你不能添加行到一个数据绑定控件 - 显而易见的原因是它不知道如何处理它们!

见这里: http://stackoverflow.com/questions/8708057/rows-cannot-be-programmatically-added-to-the-datagridviews-row -collection-when [ ^ ] - 它解释了它和解决方案。
The error message is pretty explicit:
"Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

All it's saying is that you can't add rows to a databound control - for the obvious reason that it doesn't know what to do with them!
See here: http://stackoverflow.com/questions/8708057/rows-cannot-be-programmatically-added-to-the-datagridviews-row-collection-when[^] - it explains it and the solution.


这篇关于C#Datagridview行移动另一个datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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