ListBox mulitple Selection selects all selected values

I have a problem since I just cannot find a solution that works for me. I have ListBoxone that fills DataTableas

listbox.DataSource = table;  
listbox.Displaymember = "Name";    
listbox.ValueMember = "ID";

If I now select an item in my list, I can get it as:

listbox.SelectedValue.toString();

My problem:

What should I do if I want ALL the selected values ​​from ListBoxinclude multiple selection and save them all in an array or something like that ?!

I can not use SelectedItemsbecause it does not give me the necessary information.

+4
source share
2 answers

Try the following:

var lst = listBox1.SelectedItems.Cast<DataRowView>();
foreach (var item in lst)
{
     MessageBox.Show(item.Row[0].ToString());// Or Row[1]...
}
+3
source

, , SelectedIndices:

foreach (int i in listbox.SelectedIndices)
{
    // listbox.Items[i].ToString() ...
}

:

foreach (var item in listbox.SelectedItems)
{
    MessageBox.Show(item.ToString());
}
+2

Source: https://habr.com/ru/post/1611761/


All Articles