转换列表< object>到ListView项(带有subItems) [英] Converting List<object> to ListView items (with subItems)

查看:50
本文介绍了转换列表< object>到ListView项(带有subItems)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预配置的ListView(已经设置了列标题),我要分配一个对象列表的任务,并将该列表的类的特定属性写为该列表视图的行(作为该listview的行).

I have a pre-configured ListView (with column headers already set up), that I would like to task a list of object, and write - as rows to that listview - specific attributes of the class I have a list of.

我正在尝试关注

I'm trying to follow this existing question, while it perfectly handles an item with only one column, I'm not sure how to apply multiple sub items.

我有一个包含 List< Product> 的类,该类将传递给采用该列表并将每个产品项添加到控件内ListView的UserControl.

I have a class that contains a List<Product> which is being handed off to a UserControl that takes that list and adds each product item to a ListView within the control.

虽然链接的问与答解决了此问题,但还不完善,我也不知道如何将其他属性作为子项添加到ListView中.

While the linked Q&A handles this it's not complete, and I have no idea how to get other attributes into the ListView as SubItems.

我的代码段: Products.Items.AddRange(basket.ProductList.Select(p => new ListViewItem(p.Description)).ToArray()); 其中 p List< Product>

我还希望在列表视图中显示项目数量,单价和行总数(已经在该类中计算出),但是我不确定从哪里开始.

I'd like also to have the item quantity, unit price and line total (which is worked out in the class already) in the listview, but I'm not sure where to start.

推荐答案

ListViewItem 类具有很多构造函数.最适合您的情况的是 ListViewItem构造方法(字符串[])

ListViewItem class has a lot of constructors. The most appropriate for your case is ListViewItem Constructor (String[])

公共ListViewItem(字符串[]个项目)

public ListViewItem(string[] items)

使用表示子项的字符串数组初始化ListViewItem类的新实例.

Initializes a new instance of the ListViewItem class with an array of strings representing subitems.

在您的代码段中,类似这样的

With your snippet, something like this

Products.Items.AddRange(basket.ProductList.Select(p => new ListViewItem(new string[]
{ 
    p.Description,
    p.Quantity.ToString(...),
    p.UnitPrice.ToString(...),
     ...  
})).ToArray());

请注意, ListViewItem.Text 实际上是 ListView.SubItems [0] .Text .

这篇关于转换列表&lt; object&gt;到ListView项(带有subItems)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆