【问题标题】:add value with same id in DataGridView c#在 DataGridView c# 中添加具有相同 id 的值
【发布时间】:2021-05-01 05:54:40
【问题描述】:

我想在我的 datagridview 中添加显示数据的值,因为我无法在 query 中执行此操作,因为它已加密。

显示的样本:

product sold
coke 20
coke 20

这就是我想要发生的事情:

product sold
coke 40

【问题讨论】:

  • .GroupBy() 将是您的解决方案。
  • @YongShun 谢谢先生的想法,但如何将它与datagridview整合起来
  • 如果我理解正确,您的结果是加密的,然后您必须在将它们显示在 datagridview 中之前对其进行解密?两列都是加密的还是只是名称?如果您每行使用不同的密钥 / iv,那么是的,您必须在检索结果后解密,但是,如果密钥 / iv 用于整个表,则具有“可乐”的行将加密每次都以相同的方式,允许您按列进行分组(再次假设“已售”的数字列未加密)......
  • ....如果您确实需要在将结果输入 datagridview 之前对结果进行解密,那么我建议先创建结果的数据集合,然后使用 GroupBy 之类的东西@Yong Shun 建议,并将其设置为 datagridview 的数据源。原因是,更新 datagridview 的操作成本更高。
  • @B.O.B.两者都是加密的,所以我必须解密它们并显示它。这就是为什么我想知道如何在DataGridView 中做到这一点,因为query 不是选项。

标签: c# winforms datagridview


【解决方案1】:

您可以在后面的代码中创建一个新的分组列表后创建一个新的分组列表。你可以试试这样:

        List<KeyValuePair<string, int>> productOrders = new List<KeyValuePair<string, int>>();

        // Fill list of productOrders
        productOrders.Add(new KeyValuePair<string, int>("coke", 20));
        productOrders.Add(new KeyValuePair<string, int>("coke", 20));
        productOrders.Add(new KeyValuePair<string, int>("cokeX", 20));
        productOrders.Add(new KeyValuePair<string, int>("cokeX", 20));
        productOrders.Add(new KeyValuePair<string, int>("cake", 20));
        productOrders.Add(new KeyValuePair<string, int>("cokeX", 20));


        List<KeyValuePair<string, int>> ordersByProduct = new List<KeyValuePair<string, int>>();

        foreach(var order in productOrders)
        {
            if(ordersByProduct.Where(x => x.Key == order.Key).ToList() != null && ordersByProduct.Where(x => x.Key == order.Key).ToList().Count > 0)
            {
                KeyValuePair<string, int> currentValueByProduct = ordersByProduct.Where(x => x.Key == order.Key).First();
                int combinedPrice = order.Value + currentValueByProduct.Value;
                ordersByProduct.Add(new KeyValuePair<string, int>(order.Key, combinedPrice));
                ordersByProduct.Remove(currentValueByProduct);
            }
            else
            {
                ordersByProduct.Add(new KeyValuePair<string, int>(order.Key, order.Value));
            }
        }

        // Set DataGridView to ordersBuProduct
        //return ordersByProduct;

在您的情况下,productOrders 将是解密的数据(您可能有一个 productOrders 类,您不必使用 KeyValuePair)。最后将DataGridView的数据源设置为ordersByProduct列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    相关资源
    最近更新 更多