1

I am using a Listbox on my windows application. The listbox has a multi-selection option, which users select all the roles the new user will have. My Listbox is bound to a dataset. The table is Roles, with an ID column and Description column. When I bound the listbox, I chose the data source being the dataset, the display member to be the description, the value member being the ID and the selected value being the dataset - roles.frolesidenter image description here

Now when i loop through the listbox's selecteditems, i get only the first item's values etc...

I am trying to assign an int to the current selectedvalue, to get the ID to give to the user.

 int num = 0;
 foreach (var item2 in lstRoles.SelectedItems)
 {
      num = Convert.ToInt32(lstRoles.SelectedValue);
 }

So if I select 3 roles, for instance Administrator, Capturer and Assessor, I only see the Administrator value.

How do I loop and access the next item? I'm not even using

item2 in my code. Could this be utilised?

For instance:

enter image description here

First iteration:

enter image description here

And it just stays on that selected item, never moving to the next item.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Nicholas Aysen
  • 568
  • 3
  • 18
  • 40
  • You already use it. "lstRoles.SelectedItems". If you run the iteration until finished, you can see "Administrator, Assessor, Capturer" –  Jul 11 '16 at 08:01
  • not actually. when i go through, it stays on Administrator – Nicholas Aysen Jul 11 '16 at 08:02
  • try "lstRoles.SelectedItems.Count" , it must be 3 –  Jul 11 '16 at 08:03
  • it is 3. but its not giving me anything else i could use. unless i do some if statements with the count? – Nicholas Aysen Jul 11 '16 at 08:06
  • lstRoles.SelectedItems[0] is your first selectedItem, then lstRoles.SelectedItems[1] is your second selectedItem, then lstRoles.SelectedItems[2] is your third selectedItem –  Jul 11 '16 at 08:08
  • eventually got it. Posted my answer. Thanks for the help, got me thinking on the right track – Nicholas Aysen Jul 11 '16 at 08:24

2 Answers2

1

To get list of selected values when the data source is a DataTable, you can use this code:

var selectedValues = listBox1.SelectedItems.Cast<DataRowView>()
                             .Select(dr => (int)(dr[listBox1.ValueMember]))
                             .ToList();

But in general Item of a ListBox control may be DataRowView, Complex Objects, Anonymous types, primary types and other types. Underlying value of an item should be calculated base on ValueMember.

If you are looking for a good solution which works with different settings and different data sources, you may find this post helpful. It contains an extension method which returns value of an item. It works like GetItemText method of ListBox.

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

solved it

            int num = 0;
            int count = fRoleIDs.Count;
            while (num != count)
            {
                var item = lstRoles.Items[num] as System.Data.DataRowView;
                int id = Convert.ToInt32(item.Row.ItemArray[0]);
                num += 1;
            }

This goes into the listbox's items and according to the index, gets the item as a Row. This then gets me the itemarray, which is ID and Description {0,1}

Nicholas Aysen
  • 568
  • 3
  • 18
  • 40
  • 1
    Item of a `ListBox` control may be `DataRowView`, Complex Objects, Anonymous types, primary types and other types. Underlying value of an item should be calculated base on `ValueMember`. You man find [this post](http://stackoverflow.com/a/38305363/3110834) helpful. – Reza Aghaei Jul 11 '16 at 11:32