I have bound array as datasource for ListBox . Now i need to convert listbox.Items to string of array collection.
foreach (string s1 in listBoxPart.Items)
{
clist.Add(s1);
}
here clist is string list, so how can i add ListBox.items to clist?
You can project any string
contained inside Items
using OfType
. This means that any element inside the ObjectCollection
which is actually a string
, would be selected:
string[] clist = listBoxPart.Items.OfType<string>().ToArray();
for (int a = 0; a < listBoxPart.Items.Count; a++)
clist.Add(listBoxPart.Items[a].ToString());
This should work if the items saved in the list are actualy strings, if they are objects you need to cast them and then use whatever you need to get strings out of them
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