[英]how to store multiple values from listview column to a single entry to database asp.net c#
我需要存儲從列表視圖到我的數據庫的單個條目的多列的值。
ID | Description | Price
1 | Big | 20
2 | Large | 40
3 | Small | 60
我想將值(大,大,小)存儲到單獨的表中
ID | Description | Price
1 | Big, Large, Small | 120
ive tried using
foreach (ListView s in ????? )
{
}
請幫我... :)
您可以使用以下代碼代替foreach。
class Product
{
public int ID { get; set; }
public string Description { get; set; }
public int Price { get; set; }
}
將測試數據分配到列表
List<Product> list = new List<Product>() {
new Product() { ID = 1, Description = "BBB" , Price = 10},
new Product() { ID = 2, Description = "CCC" , Price = 40},
new Product() { ID = 3, Description = "DDD" , Price = 60}};
List<Product> newList = new List<Product>();
newList.Add(new Product() { ID = list[0].ID , Description = string.Join(", ", list.Select(a => a.Description).ToArray()),
Price = list.Select(a => a.Price).Sum()});
另外,如果您想從ListView中獲取價值,則可以使用以下代碼。
var p = new Product()
{
ID = Convert.ToInt32((ListView1.Items[0].FindControl("lblID") as Label).Text),
Description = string.Join(", ", (ListView1.Items.Select(a => (a.FindControl("lblDescription") as Label).Text)).ToArray()),
Price = ListView1.Items.Select(a => Convert.ToInt32((a.FindControl("lblPrice") as Label).Text)).Sum()
};
而且,您還必須用標簽替換“ lblID”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.