Setting selected item in a ListBox without looping

后端 未结 7 1579
萌比男神i
萌比男神i 2020-12-31 19:00

I have a multi-select listbox which I am binding to a DataTable. DataTable contains 2 columns description and value.

Here\'s the listbox populating code:

         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 19:22

    I think the only way you'll be able to select multiple items is by using a foreach loop. The SelectedValue property only seems to return 1 item. If you want to select more then 1 item you'll have to use :

    var tempListBox = c As ListBox;
    if (tempListBox != null)
         (tempListBox.SelectedItems.Add(tempListBox.Items[tempListBox.FindStringExact(fieldValue)]);
    

    Also the FindStringExact doesn't search through the Value fields it only looks through the displayed text. Also to cut down on code might want to cast a new variable as a listbox so you don't keep casting C as a listbox.

提交回复
热议问题