Anyone know of a smooth way to get all of the selected items in a listbox control by using extension methods?
And, please, spare me the argument of it's irrelevant as to how one gets such a list because in the end everything uses a loop to iterate over the items and find the selected ones anyway.
* To do multiple Selections in the ListBox then just hold Ctrl key and select the items you want. In this demo, we are going to store the selected employee names that is selected from the ListBox to the database.
Try this: var lst = listBox1. SelectedItems. Cast<DataRowView>(); foreach (var item in lst) { MessageBox.
var selected = yourListBox.Items.GetSelectedItems();
//var selected = yourDropDownList.Items.GetSelectedItems();
//var selected = yourCheckBoxList.Items.GetSelectedItems();
//var selected = yourRadioButtonList.Items.GetSelectedItems();
public static class Extensions
{
public static IEnumerable<ListItem> GetSelectedItems(
this ListItemCollection items)
{
return items.OfType<ListItem>().Where(item => item.Selected);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With