Вопрос

I have a DataTable with 2 columns called "ID" and "Software" that I have used as a DataSource for a lst_Software multiselect listbox.

I'm trying to gather the ID for all the selected items in that have been selected and place that in an int[] array.

Listbox setup:

lst_Software.DataSource = software;  //software is a DataTable
lst_Software.DisplayMember = "Software";
lst_Software.ValueMember = "ID";    

I've tried below

List<int> list = new List<int>();
for (int i = 0; i < lst_Software.SelectedItems.Count; i++)
{
    list.Add(Convert.ToInt32(lst_Software.SelectedValue.ToString()));
}
int[] software = list.ToArray();

I'm finding that I'm only getting the first selected value except it will not iterate through all... I know why though. I'm not using i to get passed through inside the for loop. I'm hoping someone can give me a direction to go to iterate through all the selected values.

Thank you

Это было полезно?

Решение 2

SelectedValue is just the first selected value. You need to use SelectedItems.

Другие советы

You're using lst_Software.SelectedValue.ToString() here in the loop so it only returns the one item. You have a for loop but you're not using the index variable. However, all of this is unnecessary really all you need is;

  var items = lst_Software.SelectItems;

As that property is already the list of selected items. From there you can cast/convert them as you please.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top