[英]add value with same id in DataGridView c#
I want to add the value with of the displayed data in my datagridview because i cant do it in the with query
because it is encrypted.我想在我的datagridview中添加显示数据的值,因为我不能在with query
中这样做,因为它是加密的。
sample displayed:显示的样本:
product产品 | sold卖 |
---|---|
coke可乐 | 20 20 |
coke可乐 | 20 20 |
And here is what i want to happen:这就是我想要发生的事情:
product产品 | sold卖 |
---|---|
coke可乐 | 40 40 |
You can create a new grouped list after you have the initial one in code behind.您可以在后面的代码中有初始列表后创建一个新的分组列表。 You can try something like this:你可以尝试这样的事情:
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;
In your case productOrders would be the decrypted data (you might have a class for the productOrders you don't have to use KeyValuePair).在您的情况下,productOrders 将是解密的数据(对于您不必使用 KeyValuePair 的 productOrders,您可能有一个 class)。 In the end set the datasource of the DataGridView to the ordersByProduct list.最后将 DataGridView 的数据源设置为 ordersByProduct 列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.