0

I have a custom object like this:

    Dim Contacts As New List(Of Contact)

    Dim CurrentContact As Contact = New Contact With { _
        .Name = "Manolo", _
        .Country = "Spain", _
        .City = "Valencia"}

    Contacts.Add(CurrentContact)

The member vars are just Strings:

<Serializable()> _
Public Class Contact

#Region "Member Variables"

Private mId As System.Guid
Private mName As String
Private mCountry As String
Private mCity As String

#End Region

End Class

Now I have a Listview with "Details" mode set and with 3 columns like this:

 Name Column        |  Country Column       | City Column
 ------------------------------------------------------------------
 (Here goes Item)   | (Here goes Subitem 1) | (Here goes Subitem 2)

My question, How I can use LINQ to directly cast each value in the desired column? so the result would be this:

 Name Column        |  Country Column       | City Column
 --------------------------------------------------------
 Manolo             |  Spain                | Valencia
 Next contact Name  |  Next contact Country | Next contact City
 Next contact Name  |  Next contact Country | Next contact City
 etc...

I've tried to cast the object as "ListViewItem", "ListviewGroup" and "ListView.ListViewItemCollection" but nothing worked, I'm doing something wrong, the exception says "Value of type 'contact' cannot be converted to String":

ListView1.Items.AddRange(Contacts.Select(Function(x) New ListViewGroup(x)).ToArray)

And this else throws a "Value of type 'contact' cannot be converted to ListViewGroup"

ListView1.Items.AddRange(Contacts.Select(Function(x) New ListViewItem(x)).ToArray)
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

1 Answers1

1

Here is how to do it, you have to pass an array of string arrays to the AddRange function:

ListView1.Items.AddRange(Contacts.Select(Function(listElement) new ListViewItem(new string() { listElement.Name, listElement.Country, listElement.City })).ToArray())

not sure which without testing and I can't test right now.

See this question: Populating a listview multi-column

Community
  • 1
  • 1
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • Thanks for answer but I did not understand, I've tried to test the code but does not work, I don't have any listviewitem already set, I need to add a range (a List(of Contact)) into an empty listview, which has 3 columns, and the object has 3 members (contact.name, contact.country, contact.city), if you need more information just tell me to update the question with the "Contact" class content, but I tell you that the class only has string properties and members. PS: Forgive my English – ElektroStudios Oct 14 '13 at 18:15
  • can you be more specific please? – ElektroStudios Oct 14 '13 at 18:25
  • 1
    @ElektroHacker - See edit. Looked again at the documentation – Hogan Oct 14 '13 at 18:26
  • Thankyou again but no way it won't work, i show you the exception message: Overload resolution failed because no accessible 'AddRange' can be called with these arguments: Value of type '1-dimensional array of 1-dimensional array of String' cannot be converted to 'System.Windows.Forms.ListView.ListViewItemCollection'. – ElektroStudios Oct 14 '13 at 18:28