【问题标题】:How to make DataGridView to "increase" and not duplicate如何使 DataGridView “增加”而不是重复
【发布时间】:2021-08-21 00:36:43
【问题描述】:

我有一个带有 3 个参数的方法,可以向 DataGridView 添加行:

public void DGVadd(string item, int count, int price)
        {
            DataGrid.Rows.Add(item, count, price);
            DataGrid.Update();
        }

我只需单击按钮即可调用该方法:

private void button1_Click(object sender, EventArgs e)
        {
            DGVadd("item1", 1, 10);
        }

问题是:我应该怎么做才能使同名的Items不会在DataGridView中占用多行,只有1个并使“计数”和“价格”增加?

感谢您的宝贵时间!

【问题讨论】:

标签: c# winforms


【解决方案1】:

您需要检查 DataGrid 的行并检查该项目是否存在。

很久没用WinForms了,不过应该是这样的(它只是一个伪代码)

DGVadd(string item, int count, int price)
{
    bool newRow = true;
    foreach (DataRow row in DataGrid.Rows)
    {
        if (row.item.Equals(item)
        {
            row.count += count;
            row.price += price;
            newRow = false;
        }
    }
    if (newRow)
    {
        DataGrid.Rows.Add(item, count, price);
    }
    DataGrid.Update();
}

请记住,这只是一个伪代码,其中包含我所指的逻辑

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多