Skip to content

Inserting in to a list box. Insertions not appearing.

Paul Chambers

Asked Oct 23, 2010, 2:00 PM EDTSolved
Share
LinkedInX
Hi All, 

I am trying to get the following to work: 

C#
  TexationJobsEntities de = new TexationJobsEntities();

            var operatives = from o in de.Operatives
                             select new {o.OperativeID, o.First_Name, o.Last_Name};

            foreach (var operative in operatives)
            {
                operativesListBox.Items.Insert(operative.OperativeID, operative.First_Name + " " + operative.Last_Name);
            }

Its very weird as the list box remains empty. but the following works fine (apart from the fact that the entries are not in sync with the source database)

C#
  TexationJobsEntities de = new TexationJobsEntities();

            var operatives = from o in de.Operatives
                             select new {o.OperativeID, o.First_Name, o.Last_Name};

            foreach (var operative in operatives)
            {
                operativesListBox.Items.Add(operative.First_Name + " " + operative.Last_Name);
            }


anyone have any ideas ?

Thanks in advnace

Regards
Ore

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    ✓ Accepted answer0
    Another way of doing...
     
    Code
    var operatives = from o in de.Operatives
            select new
            {
                OID = o.OperativeID,
                FName_LName = o.First_Name + " " + o.Last_Name
            };
    Code
    operativesListBox.DataSource = operatives;
    operativesListBox.DataTextField = "FName_LName";
    operativesListBox.DataValueField = "OID";
    operativesListBox.DataBind();
    0
  • Suthish Nair
    0
    Put a break point on code line
    Code
    foreach (var operative in operatives)
    Code
    and check the value of operatives.
    0
  • 0
    Hi Suthish Nair,
     
    Thanks for the replies. I tried the breakpoint previously and everything seemed to be working but the list box just remained blank.
    I tried your second solution with a little bit of tweaking it seems to be working the way I want!!
    Thanks for your help!
     
    regards
    0