在 DataGridView c# 中添加具有相同 ID 的值
add value with same id in DataGridView c#
我想在我的 datagridview 中添加显示数据的值,因为我不能在 query
中添加它,因为它是加密的。
显示的示例:
product
sold
coke
20
coke
20
这就是我想要发生的事情:
product
sold
coke
40
您可以在代码中拥有初始分组列表后创建一个新的分组列表。你可以尝试这样的事情:
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 将是解密的数据(您可能有一个 class 的 productOrders,您不必使用 KeyValuePair)。最后将 DataGridView 的数据源设置为 ordersByProduct 列表。
我想在我的 datagridview 中添加显示数据的值,因为我不能在 query
中添加它,因为它是加密的。
显示的示例:
product | sold |
---|---|
coke | 20 |
coke | 20 |
这就是我想要发生的事情:
product | sold |
---|---|
coke | 40 |
您可以在代码中拥有初始分组列表后创建一个新的分组列表。你可以尝试这样的事情:
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 将是解密的数据(您可能有一个 class 的 productOrders,您不必使用 KeyValuePair)。最后将 DataGridView 的数据源设置为 ordersByProduct 列表。