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. C#
  4. loading a combobox without repitition, how to??

loading a combobox without repitition, how to??

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpdatabase
7 Posts 4 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.
  • N Offline
    N Offline
    Nada Adel
    wrote on last edited by
    #1

    Hi, I want to check if an item read from a database exists in the combobox before I add it to the combobox, so that the item is not repeated in the combox. For example I want to check for the reader["Area"] doesn't exist in "comboArea", if it doesn't exist, then add it to the combobox. how can I translate that in C# language?? I'm not sure how it's written. If anybody has anybody has any idea how to do this, plz send it.

    RaviBeeR D 2 Replies Last reply
    0
    • N Nada Adel

      Hi, I want to check if an item read from a database exists in the combobox before I add it to the combobox, so that the item is not repeated in the combox. For example I want to check for the reader["Area"] doesn't exist in "comboArea", if it doesn't exist, then add it to the combobox. how can I translate that in C# language?? I'm not sure how it's written. If anybody has anybody has any idea how to do this, plz send it.

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      Use a Hashtable to create a unique set representation of your data and store that in the ComboBox or just use myComboBox.Items.Contains() to check for an item's presence before adding it. /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      N 1 Reply Last reply
      0
      • RaviBeeR RaviBee

        Use a Hashtable to create a unique set representation of your data and store that in the ComboBox or just use myComboBox.Items.Contains() to check for an item's presence before adding it. /ravi

        This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        N Offline
        N Offline
        Nada Adel
        wrote on last edited by
        #3

        thnx for the fast reply, I used this function: myComboBox.Items.Contains() but the problem is that it takes ListItem, and in my case I want to check reader["Area"] retrieved from a database, so it refuses this as an argument to the function..so is there any other way??

        M RaviBeeR 2 Replies Last reply
        0
        • N Nada Adel

          thnx for the fast reply, I used this function: myComboBox.Items.Contains() but the problem is that it takes ListItem, and in my case I want to check reader["Area"] retrieved from a database, so it refuses this as an argument to the function..so is there any other way??

          M Offline
          M Offline
          MoustafaS
          wrote on last edited by
          #4

          You may cast reader["Area"] to ListItem also you may add all fields while using reader, then filter the combobox on your own.

          ------------------------------
          "The Soapbox has been so ..."

          1 Reply Last reply
          0
          • N Nada Adel

            thnx for the fast reply, I used this function: myComboBox.Items.Contains() but the problem is that it takes ListItem, and in my case I want to check reader["Area"] retrieved from a database, so it refuses this as an argument to the function..so is there any other way??

            RaviBeeR Offline
            RaviBeeR Offline
            RaviBee
            wrote on last edited by
            #5

            Nada Adel wrote:

            I used this function: myComboBox.Items.Contains() but the problem is that it takes ListItem,

            Huh? Does this work? (Obviously I'm writing this out of context since I'm not familiar with your application, but hopefully you get what I mean).

            myComboBox.Items.Clear();
            foreach (Object o in objectsRetrievedFromDatabase) {
            if (!myComboBox.Items.Contains (o)) {
            myComboBox.Items.Add (o);
            }
            }

            /ravi

            This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

            N 1 Reply Last reply
            0
            • RaviBeeR RaviBee

              Nada Adel wrote:

              I used this function: myComboBox.Items.Contains() but the problem is that it takes ListItem,

              Huh? Does this work? (Obviously I'm writing this out of context since I'm not familiar with your application, but hopefully you get what I mean).

              myComboBox.Items.Clear();
              foreach (Object o in objectsRetrievedFromDatabase) {
              if (!myComboBox.Items.Contains (o)) {
              myComboBox.Items.Add (o);
              }
              }

              /ravi

              This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

              N Offline
              N Offline
              Nada Adel
              wrote on last edited by
              #6

              thnx everybody:)

              1 Reply Last reply
              0
              • N Nada Adel

                Hi, I want to check if an item read from a database exists in the combobox before I add it to the combobox, so that the item is not repeated in the combox. For example I want to check for the reader["Area"] doesn't exist in "comboArea", if it doesn't exist, then add it to the combobox. how can I translate that in C# language?? I'm not sure how it's written. If anybody has anybody has any idea how to do this, plz send it.

                D Offline
                D Offline
                Drew McGhie
                wrote on last edited by
                #7

                As an alternative to what people have said, is there any sort of pre-population of the items in the combobox? If you're just populating the CB from items in a database, and you don't want things to repeat, just use DISTINCT in the query from the database (I'm assuming SQL here), and just use the combobox.displaymember, combobox.valuemember, and combobox.datasource to set it up.

                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