Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I add items to ListBox like from a string using "," as a delimiter. Like suppose I want to add first names to a ListBox. How can I add these items from a string like "rupa,anitha,latha,rani"?

Can any body help me?

Thanks in advance!
Posted
Updated 21-Jan-11 3:20am
v3

ListBox lst = new ListBox();
        lst.Items.Add(new ListItem("rupa"));
        lst.Items.Add(new ListItem("anitha"));
        lst.Items.Add(new ListItem("latha"));
        lst.Items.Add(new ListItem("rani"));


That is what you looking for ?

Ok now as manfred suggested I can revise my answer like

string str = "hiren,manfred,you,me,all,cp";
string[] items = str.split(',');

for(int i=0;i<items.length;i++)>
{
lst.add(new ListItem(string[i]));
}
 
Share this answer
 
v2
Comments
Rupa1 21-Jan-11 9:08am    
not line by line sir in listbox like rupa,anitha,latha,rani in one row
Hiren solanki 21-Jan-11 9:11am    
Then what's problem with

lst.Items.Add(new ListItem("rupa,anitha,latha,rani")); ?
Rupa1 21-Jan-11 9:13am    
dynamically i want to add that sir..static lst.Items.Add(new ListItem("rupa,anitha,latha,rani")); it will work
Hiren solanki 21-Jan-11 9:15am    
what's a source for taking data dynamic ? can you please clarify the whole scenario ?
Manfred Rudolf Bihy 21-Jan-11 9:18am    
I edited OP's question to make it a little clearer what OP wanted.
Try this:

C#
public ListBox AddItemsToListBoxFromString(ListBox lb, String strItems, char[] separators)
{
    String[] items = strItems.Split(separators);
    foreach(String item in items)
    {
        lb.Items.Add(item.Trim());
    }
    return lb;
}


A typical call from a code behind file might look like this:
C#
...
AddItemsToListBoxFromString(listBox1, "rupa,anitha,latha,rani", new char[] { ',' });
...


Hope this helps you!

Best Regards,
Manfred
 
Share this answer
 
v2
Comments
Hiren solanki 21-Jan-11 9:22am    
Thanks for correcting question. Now you wins. +5.
Sergey Alexandrovich Kryukov 21-Jan-11 13:41pm    
Very good, my 5, but please see my minor improvement (a separate answer)
Formally, there are correct answers already.

I would add advice a minor improvement to all the presented codes:

C#
string[] items = source.Split(
    new char[] { ' ', ',' },
    StringSplitOptions.RemoveEmptyEntries);


It allows for more relaxed human-readable requirements for the format of the input.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 22-Jan-11 10:17am    
Cood call! 5+ RemoveEmptyEntries is a good option in this case. :)
Use following code example to add items in to listbox

listBox1.Items.Add("rupa");
 
Share this answer
 
Comments
Rupa1 21-Jan-11 9:10am    
not line by line rupa,xxxx,yyyy,zzzzz like sir..........
raghu.g 21-Jan-11 9:12am    
how do you retrieve the names?
do you have the first name as a collection or what..??
Rupa1 21-Jan-11 9:19am    
ya i ve gridview in that i will check checkbox that all items i want be add in listbox rupa,xxx,yyy...........
raghu.g 21-Jan-11 9:37am    
on click of check box in the grid view event add following code

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(GridViewRow gr in GridView1.Rows)
{
if (((CheckBox)gr.FindControl("CheckBox2")).Checked == true)
{
sb.Append(gr.Cells[2].Text+",");

// 2 here the position of name in the grid view
}
}
ListBox1.Items.Clear();
ListBox1.Items.Add(sb.ToString());
}

I think this is what you are looking for
Rupa1 21-Jan-11 22:54pm    
thx sir exactly i am looking for that ...............really thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900