1
Answer

How to Retrieve a ListviewItem

Photo of Administrator

Administrator

22y
3.9k
1
Hi, I am new to c# (moving across from the VB6 world), and have a question which I haven't been able to figure out the answer to. Basically put, is there any way you can retrieve a ListviewItem by a key value (NOT THE INDEX). In VB this is very easy, for example; Dim loItem As MSComctlLib.ListItem With ListView1 .ListItems.Add , "prussell", "Paul Russell" .ListItems.Add , "dburton", "Dexter Burton" .ListItems.Add , "jthornton", "James Thornton" End With Set loItem = ListView1.ListItems("dburton") If Not loItem Is Nothing Then Debug.Print "Found Dexter Burton" End If I know that I can work around the problem by looping through the ListviewItems in the ListView.Items collection and comparing one of the columns to a key value, but this is a very inefficient way of doing this. Is there anyway that this can be done in c#? Any help would be greatly appreciated NBK

Answers (1)

0
Photo of Administrator
Admin 2.3k 1.3m 22y
MSComctlLib.ListItem loItem; ListView1.ListItems.Add( , "prussell", "Paul Russell"); ListView1.ListItems.Add( , "dburton", "Dexter Burton"); ListView1.ListItems.Add( , "jthornton", "James Thornton"); loItem = ListView1.ListItems["dburton"]; if(!loItem == null) { debug.print("Found Dexter Burton"); } This is the basic syntax you need to use, I'm not sure if it's all correct as I develop web and not windows interfaces. Good Luck