Populate listbox

This cant be that hard but how can I populate a listbox. I'm using LINQ but it shouldnt matter.

If I use listbox.Items.Add(whatever)..this works but i need the "key" value also.

if I do lisbox.Attributes.Add(key,value).. i dont get anything in the list box. This is what i have.
var siteNames = from sValues in dc.LSite
                          where !sValues.Id.Equals(Guid.Empty)
                          select sValues;
 
        foreach (var p in siteNames)
        {
            //lbSites.Items.Add(p.Name);
            lbSites.Attributes.Add(p.Code, p.Name);
 
        }

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?
 
Albert Van HalenConnect With a Mentor Analyst developerCommented:
Use this
lbSites.Items.Add(new ListItem(p.Code, p.Name)); // first parameter si text, second parameter is value

Open in new window

0
 
CamilliaAuthor Commented:
let me try.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.