【问题标题】:how to store multiple values from listview column to a single entry to database asp.net c#如何将listview列中的多个值存储到数据库asp.net c#的单个条目
【发布时间】:2013-09-19 13:21:48
【问题描述】:

我需要将列表视图中多列的值存储到我的数据库的单个条目中。 例子

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 ????? )
{
}

请帮帮我... :)

【问题讨论】:

    标签: c# asp.net linq listview


    【解决方案1】:

    您可以使用以下代码代替 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”。

    【讨论】:

    • 我想从 ListView 中获取值。我该怎么做?这些值已经显示到 ListView... :)
    猜你喜欢
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多