el ninho
el ninho

Reputation: 4233

ASPxListBox items to string

This should be easy, but I'm failing to do this. I have ASPxListBox 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();

(I asked similar question before, but as I havent mention that this is ASPxListBox, suggestions I got wont works.

So, this is Visual Studio 2008, Devexpress 9.3 and .NET framework 3.5

Thanks.

Upvotes: 0

Views: 4480

Answers (3)

Mikhail
Mikhail

Reputation: 9300

@keyboardP: your solution is rather ok on my mind, so +1.

Moreover, you can find a similar solution in the DevExpress code central:

http://www.devexpress.com/Support/Center/e/E2625.aspx

ASPxListBox listBox = instance;

string selectedItemsAsString = string.Empty;

foreach (ListEditItem item in listBox.SelectedItems)
    selectedItemsAsString += item.Value + ";";

if (selectedItemsAsString.Length > 0)
    selectedItemsAsString = selectedItemsAsString.Trim(new char[] { ';' });

Upvotes: 1

DotNetUser
DotNetUser

Reputation: 6612

I am not familiar with aspxlistbox but You can write something like this-

    String Finaloutput="";
    for (int i = 0; i < lb1.Items.Count; i++)
    {
       string output= lb1.Items[i].Selected ? lb1.Items[i].Text + "," : "";
       Finaloutput += output;
    }
    TextBox1.Text = Finaloutput.TrimEnd(',');

Upvotes: 0

keyboardP
keyboardP

Reputation: 69372

I'm not familiar with ASPxListBox, but couldn't you just loop through and then remove the last comma by getting the substring?

 string csvList = sb.ToString();
 TextBox1.Text = csvList.Substring(0, csvList.LastIndexOf(','));

Upvotes: 2

Related Questions