数据网格视图:将插入特定列复制到另一个 DGV

本文关键字:复制 DGV 另一个 插入 网格 数据网 视图 数据 | 更新日期: 2023-09-27 18:30:42

我有两个数据网格视图(dgv),有两个单独的数据源,第一个dgv显示N年的数据,第二个显示N-1年的数据(以及其他信息:客户,产品等。除金额外,两个DGV的相同列和相同信息)。为了计算一些统计变量,我需要从 dgv 2 复制列"金额"并将其插入到 dgv 1 中。这个想法是使用N年和N-1年的数据进入dgv知道如何做到这一点,谢谢。

数据网格视图:将插入特定列复制到另一个 DGV

这是你想要的吗?

// create a new column named Amount in your dgv at specified index (index 0 in my case)
int newColumnIndex = 0;
dgv2.Columns.Insert(newColumnIndex , new DataGridViewTextBoxColumn { Name = "Amount" });
// get an index of the Amount column in your other dgv
var index = dgv1.Columns["Amount"].Index;
// copy all items from dgv1 in that column to new column in dgv2
for (int i = 0; i < dgv1.Rows.Count; i++)
    dgv2.Rows[i].Cells[newColumnIndex].Value = dgv1.Rows[i].Cells[index].Value;