Listbox items to string

10,053

You could try (uses ASPxListBox.SelectedItems):

var selectedItems = 
    String.Join(",", lb1.SelectedItems.Select(i => i.ToString()));

Or if you're using an older version of .NET without LINQ:

List<string> values = new List<string>();

foreach(object o in lb1.SelectedItems)
    values.Add(o.ToString());

string selectedItems = String.Join(",", values);
Share:
10,053
el ninho
Author by

el ninho

Junior developer

Updated on June 14, 2022

Comments

  • el ninho
    el ninho almost 2 years

    This should be easy, but I'm failing to do this. I have listbox with checkbox option turned on. So I'd like to have selected items in one string. Like this:

    item1,item4,item9
    

    and so on.

    Notice that they should be divided by "," and last comma deleted.

    Tried something like this, but won't work:

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < lb1.Items.Count; i++)
        sb.Append(lb1.Items[i].Selected ? lb1.Items[i].Text + "," : "");
    TextBox1.Text = sb.ToString();
    
  • el ninho
    el ninho over 12 years
    No luck with any of those. I'm using VS2008 and devexpress 9.
  • Justin Niessner
    Justin Niessner over 12 years
    @elninho - What happened when you tried the second suggestion? No result? Errors? Syntax issues? Can't help if you're not a little more specific.