[英]add value with same id in DataGridView c#
我想在我的datagridview中添加顯示數據的值,因為我不能在with query
中這樣做,因為它是加密的。
顯示的樣本:
產品 | 賣 |
---|---|
可樂 | 20 |
可樂 | 20 |
這就是我想要發生的事情:
產品 | 賣 |
---|---|
可樂 | 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 將是解密的數據(對於您不必使用 KeyValuePair 的 productOrders,您可能有一個 class)。 最后將 DataGridView 的數據源設置為 ordersByProduct 列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.