c# - 如何将选定的行从一个 datagridview 传输到另一个?
问题描述
我尝试将选定的行从一个 datagridview 传输到另一个。我使用组合框文本更改事件将选定的数据加载到数据网格。因此,传输代码需要dataGridView1.AllowUserToAddRows = false;
为两个dataGridView
s 做。但由于使用组合框 textchange 事件,我无法禁用添加新行。如何解决这个问题?我使用以下代码来传输值:
private void btnaddtoanother_Click(object sender, EventArgs e)
{
for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++)
{
bool rowAlreadyExist = false;
bool checkedCell = (bool)dataGridView2.Rows[i].Cells[0].Value;
if (checkedCell == true)
{
DataGridViewRow row = dataGridView2.Rows[i];
if (dataGridView1.Rows.Count != 0)
{
for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
{
if (row.Cells[0].Value.ToString() == dataGridView1.Rows[j].Cells[0].Value.ToString())
{
rowAlreadyExist = true;
break;
}
}
if (rowAlreadyExist == false)
{
dataGridView2.Rows.Add(row.Cells[0].Value.ToString());
dataGridView2.Rows.Add(row.Cells[1].Value.ToString());
dataGridView2.Rows.Add(row.Cells[3].Value.ToString());
}
}
else
{
dataGridView2.Rows.Add(row.Cells[0].Value.ToString());
dataGridView2.Rows.Add(row.Cells[1].Value.ToString());
dataGridView2.Rows.Add(row.Cells[3].Value.ToString());
}
}
}
}
解决方案
在尝试将其添加到新网格之前,先从网格中删除该行。
foreach (GridViewRow row in fromGridView.SelectedRows.OfType<DataGridViewRow>().ToArray())
{
fromGridView.Rows.Remove(row);
toGridView.Rows.Add(row);
}
推荐阅读
- asp.net-core-webapi - 如何更改 ASP.NET Core API 中的默认响应内容类型?
- sql - 不满足条件时,Postgresql case 语句返回空行
- perl - 如何在 perl 编程中非常快速地读取 .gz 文件中的数据
- sql - 用于查找同一站点在 +/- 1 天内记录的作业的 SQL 代码
- c++ - 即使没有返回语句也返回 C++ 值
- c# - 核心 3.1 - 绑定 Body 中的一些属性和 Header 中的其他一些属性
- angular - RXJS BehaviorSubject getValue vs value
- swift - 调整 SKView 大小时的奇怪行为 - 节点随机缩放
- r - knitr 钩子以指示子 rmd 状态
- extjs - 具有多个来源的 Extjs PropertyGrid