C#如何向listview中的一列添加LIST集合中的OBJECT数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#如何向listview中的一列添加LIST集合中的OBJECT数据相关的知识,希望对你有一定的参考价值。

public frmTaskAllocation(List<IFeature> pFeatureListSearchArea, List<IFeature> pFeatureListTarget)

InitializeComponent();
foreach (IFeature p in pFeatureListSearchArea)

this.comboBox1.Items.Add(p.Value[p.Fields.FindField("FID")]);

foreach (IFeature p in pFeatureListTarget)

ListViewItem item = new ListViewItem();
//items.add();




这是我做的两个遍历,第一个遍历就是像combobox中遍历并加入集合中的数据。
同样的现在我要向列中添加p.Value[p.Fields.FindField("MMSI")数据,如何添加呢?

直接将listview的ItemSource设置为你的集合 你那种方法,一个一个添加,那集合类型你转换一下呗 Tostring()或者Conver.Tostring()追问

可我要遍历的是这个集合中的某一个属性,然后将这属性的所有值传到LISTVIEW中,所以我才用p.Value[p.Fields.FindField("FID")]这种写法,假如把这集合转换成string就不好实施了吧?

追答

传什么就转什么

参考技术A 你这种数据有什么不一样吗?追问

和上面的一样,但是我试过没法直接添加到SUBITEMS.ADD()中

追答

报错?

追问

因为p.Value[p.Fields.FindField("MMSI")这串好像是OBJECT的,但item.SubItems.Add()是添加STRING类型的,所以现在不知道怎么写

在c#如何将listview中的数据保存到数据库中的表中

感激

private void listView1_Click(object sender, EventArgs e)//listview点击事件

if (this.listView1.SelectedItems == null) return;
ListViewItem item = this.listView1.SelectedItems[0];//选中的ltem

if (this.comboBox1.SelectedIndex == -1) return;
if (item == null) return;

//把每一项里的值取出来
string a = item.SubItems[0].Text.ToString();
string b = item.SubItems[1].Text.ToString();
string c = item.SubItems[2].Text.ToString();
string d = item.SubItems[3].Text.ToString();

然后你用ADO连接数据库,写insert语句,把取出来的值当参数传给SQL语句。就可以了 思路大概是这样的,希望你帮助你~
参考技术A Ctrl+C
Ctrl+V

以上是关于C#如何向listview中的一列添加LIST集合中的OBJECT数据的主要内容,如果未能解决你的问题,请参考以下文章

在c#如何将listview中的数据保存到数据库中的表中

c#中 如何用listview显示图片

c#将list集合直接绑定到listview控件

如何在一个DataGridView中的一列添加DateTimePicker控件 C#

c#如何向一个集合中添加元素

如何在 C#中的listView 控件中显示图片?