DennG
DennG

Reputation: 91

Show listView.Items in a ComboBox

How can I show the listView.Items on Form2 in a ComboBox on Form1, and I want to use all data (subitems) from the selected Item.

How can I do that?

Upvotes: 1

Views: 2159

Answers (1)

Codesleuth
Codesleuth

Reputation: 10541

Form1.comboBox.Items.AddRange(
    Form2.listView.Items.Cast<ListViewItem>().Select(a => a.Text));

This will simply copy the text of the ListViewItem to the combo box.

For all subitems, it gets a bit more complex:

Form1.comboBox.Items.AddRange(
    Form2.listView.Items.Cast<ListViewItem>().Select(
    a => string.Join(", ", a.SubItems
        .Cast<System.Windows.Forms.ListViewItem.ListViewSubItem>()
        .Select(s => s.Text).ToArray())).ToArray());

This uses LINQ to get an array of text values from the subitems of each item to be joined together with ", ", and adds each concatenated string list to the ComboBox

Upvotes: 1

Related Questions