Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Add item to a listbox that is bind to a DataSource

Add item to a listbox that is bind to a DataSource

Scheduled Pinned Locked Moved Windows Forms
wpfwcfquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    imak
    wrote on last edited by
    #1

    I have a simple list box that contains some string values. This list box is initially populated by binding to a collection (via DataSource field). However later user will be able to add some values into it also. When I just do a listbox.Items.Add(mystring), this new values never show up in the listbox itself. Any ideas what I am doing wrong? Or may be there are standard patterns for dealing with this type of situation?

    D 1 Reply Last reply
    0
    • I imak

      I have a simple list box that contains some string values. This list box is initially populated by binding to a collection (via DataSource field). However later user will be able to add some values into it also. When I just do a listbox.Items.Add(mystring), this new values never show up in the listbox itself. Any ideas what I am doing wrong? Or may be there are standard patterns for dealing with this type of situation?

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      When bindigng to a collenction, you have to add the item to the collection not the listbox.Items. Watch out for Exceptions!!!

      listBox1.DataSource = yourListOrCollection;

      //to add an item, unbound the listBox
      listBox1.DataSource=null;

      //add the item to the collection
      yourListOrCollection.Add("Something");
      //and set the updated collection as the data source

      listBox.DataSource = yourListOrCollection;

      I 1 Reply Last reply
      0
      • D Dan Mos

        When bindigng to a collenction, you have to add the item to the collection not the listbox.Items. Watch out for Exceptions!!!

        listBox1.DataSource = yourListOrCollection;

        //to add an item, unbound the listBox
        listBox1.DataSource=null;

        //add the item to the collection
        yourListOrCollection.Add("Something");
        //and set the updated collection as the data source

        listBox.DataSource = yourListOrCollection;

        I Offline
        I Offline
        imak
        wrote on last edited by
        #3

        Thanks for the reply. Problem is that my collection is actually an entity collection that is returning bunch of columns from table and I am just using one of the column to populate this listbox. I therefore not sure if I can add this item into the collection.

        D 1 Reply Last reply
        0
        • I imak

          Thanks for the reply. Problem is that my collection is actually an entity collection that is returning bunch of columns from table and I am just using one of the column to populate this listbox. I therefore not sure if I can add this item into the collection.

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          sure there is. nothing is impossible. You could trick/force something like that in many ways. 0) Add a whole item to the collection with default/empty values for the not used cols(delete them after words) 1) Createa a separate array or list of strings from that column ...

          I 1 Reply Last reply
          0
          • D Dan Mos

            sure there is. nothing is impossible. You could trick/force something like that in many ways. 0) Add a whole item to the collection with default/empty values for the not used cols(delete them after words) 1) Createa a separate array or list of strings from that column ...

            I Offline
            I Offline
            imak
            wrote on last edited by
            #5

            Got it. Its working for me now. Thanks for your help

            D 1 Reply Last reply
            0
            • I imak

              Got it. Its working for me now. Thanks for your help

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #6

              np

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups