在DataGridView c#中添加具有相同id的值



我想在我的数据网格视图中添加显示数据的值,因为我无法在query中添加,因为它是加密的。

显示的样本:

产品已售出
焦炭 20
焦炭 20

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

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列表。

最新更新