【问题标题】:DataGridView: Copy Insert specific Column to another DGVDataGridView:将插入特定列复制到另一个 DGV
【发布时间】:2013-09-08 15:38:51
【问题描述】:

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

【问题讨论】:

  • 您使用的是 Windows 窗体吗?

标签: c# winforms datagridview


【解决方案1】:

这是你想要的吗?

// 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;

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多