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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.