c# - 如何根据另一行数据 GridView 汇总一行?

标签 c# winforms datagridview

我是新来的,想请教一下。我有

enter image description here

我想在 dataGridOne 中添加一行时自动填充我的 dataGridTwo。在 dataGridTwo Quantity 单元格中,dataGridOne Quantity 单元格与相同的 dataGridOne Shipping Container 单元格相加。

enter image description here

最佳答案

您需要为“运输容器”列中的单元格处理 CellEndEdit 事件:

dataGridOne.CellEndEdit += DataGridOne_CellEndEdit;

想法是每当“运输容器”列中的单元格发生更改时,您将遍历所有行,计算每个容器的出现次数。然后您将使用这些计数更新 dataGridTwo;根据需要添加任何缺少的容器。

不要带走所有的乐趣,代码/算法:

private void DataGridOne_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    // ColumnName = the Name of the Shipping Container Column
    if (dataGridOne.Columns[e.ColumnIndex] == dataGridOne.Columns["ColumnName"])
    {
        Dictionary<string, int> sums = new Dictionary<string, int>();

        // For each row that's not the new row in dataGridOne
        //     key = the value in cell["ColumnName"] of the row (i.e. "Mega Bags")
        //     if sums contains the key, increment sums value
        //     else sums value is 1

        // For each row that's not the new row in dataGridTwo
        //     key = the value in cell["ColumnName"] of the row (i.e. "Mega Bags")
        //     if sums contains the key, set the cell value to sums value
        //     else set the cell value to 0
        //     remove the key from sums

        // For each remaining KeyValuePair in sums
        //     add a new row to dataGridTwo using (key, CRIS ID, value)
    }
}

关于c# - 如何根据另一行数据 GridView 汇总一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34985082/

相关文章:

c# - 为 WPF 动画制作 C# 库线程安全

c# - 使用秒表对应用程序使用情况进行基准测试

c# - 为什么我们这里没有死锁?

c# - 如何在 C# 控制台应用程序中从 Static void main 调用其他类的方法?

c# - 在 Windows 窗体中调用公共(public)方法

c# - 从 C# 中的 DataGridView 获取选定行的第一个单元格

c# - 使用 DataTable 将数据加载到 DataGridView 的进度条

c# - TextBox 在 '\0' 字符处截断字符串

c# - 如何在 Windows 窗体 C# 中为文本框提供渐变颜色

C# GUI 可编辑 DataGridView