从listView控件中获取值 [英] get the value from the listView control

查看:188
本文介绍了从listView控件中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我将数据和值导入listView的方式。



this is how i import the data and the value into the listView.

this.listView.Items.Add("description", "ID");




listView.SelectedItems[0].Text 



以上代码只返回描述而不是ID ,我怎样才能获得ID的值?


code above only return me the "description" not "ID", how can i get the value of the "ID"?

推荐答案

参见http://msdn.microsoft.com/en-us/library/17fe5a7z.aspx [ ^ ]和 http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selecteditems.aspx [ ^ ]。


当你执行:
this.listView.Items.Add("description", "ID");

一个新的ListViewItem被创建并添加到ListView控件的ListViewItems集合中,其'Text属性设置为description,其'ImageKey属性设置为ID



当您使用ListView的Items.Add方法时,您需要查看可用的选项:[ ^ ]。



另一个ListViewItem属性,可用于保存字符串是'名称。 Ms的文档可能有点令人困惑,因为经常显示使用'key作为参数:在实践中'键是'ListViewItem的'Name属性。



除非您实际上在LIstView中使用图像,否则我建议您使用'ImageKey和'ImageIndex属性,但使用'名称和'文本属性。是的,你可以逃脱使用这些领域,但我认为这将导致将来出现问题。



如果你真的需要关联很多东西使用ListViewItem,您可以使用其'Tag属性来存储对任何内容的引用,但这需要将存储在Tag中的对象转换回您存储在其中的任何类型的类型:替代方法是子类ListView并添加其他字段,或其他任何字段。

A new ListViewItem is created and added to the ListViewItems Collection of the ListView Control with its 'Text property set to "description," and its 'ImageKey property set to "ID"

You need to review the options available when you use the ListView's Items.Add method: [^].

Another ListViewItem property you can use to hold a string is 'Name. Ms's documentation can be a little confusing since the often show the use of the word 'key as a parameter: in practice 'key is the 'Name property of the ListViewItem.

Unless you are actually using images with your LIstView, I recommend you do not make use of the 'ImageKey and 'ImageIndex properties, but use 'Name, and 'Text properties. Yes, you can get away with using those fields, but I think it will lead to problems in the future.

If you really need to associate a "lot of stuff" with a ListViewItem, you can use its 'Tag property to store an reference to whatever, but that requires converting the object stored in the Tag back into the Type of whatever you stored in it: an alternative to that is to sub-class ListView and add additional fields, or whatever.


这篇关于从listView控件中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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