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. How to bind a list to a combobox

How to bind a list to a combobox

Scheduled Pinned Locked Moved C#
helptutorial
9 Posts 3 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.
  • B Offline
    B Offline
    baranils
    wrote on last edited by
    #1

    Hello I'm trying to bind a list to a combobow but without effective result class Rue { public int Id; public string Nom; }; List<Rue> Rues = new List<Rue>(); Then I'm trying to fill and bind that list to a Combo Rues.Clear(); readRues("1000",Rues); cmb_rues.DataSource = Rues; cmb_rues.DisplayMember = Rues[0].Nom; The Combo displays things but not what I expect It shows App.Form+Rue App.Form+Rue App.Form+Rue etc.. Thanks for any help !

    C C 2 Replies Last reply
    0
    • B baranils

      Hello I'm trying to bind a list to a combobow but without effective result class Rue { public int Id; public string Nom; }; List<Rue> Rues = new List<Rue>(); Then I'm trying to fill and bind that list to a Combo Rues.Clear(); readRues("1000",Rues); cmb_rues.DataSource = Rues; cmb_rues.DisplayMember = Rues[0].Nom; The Combo displays things but not what I expect It shows App.Form+Rue App.Form+Rue App.Form+Rue etc.. Thanks for any help !

      C Offline
      C Offline
      C Scharbe
      wrote on last edited by
      #2

      I think you need to write: cmb_rues.DataSource = Rues; cmb_rues.DisplayMember = "Nom"; cmb_rues.ValueMember = "Id";

      B 1 Reply Last reply
      0
      • C C Scharbe

        I think you need to write: cmb_rues.DataSource = Rues; cmb_rues.DisplayMember = "Nom"; cmb_rues.ValueMember = "Id";

        B Offline
        B Offline
        baranils
        wrote on last edited by
        #3

        Hello I've try it too !! Same result BUT If I declare the ValueMember I got a message cannot bind to the new displaymember

        C 1 Reply Last reply
        0
        • B baranils

          Hello I've try it too !! Same result BUT If I declare the ValueMember I got a message cannot bind to the new displaymember

          C Offline
          C Offline
          C Scharbe
          wrote on last edited by
          #4

          Try this: class Rue { public int Id; public string Nom; public override string ToString() { return Nom; } };

          B 1 Reply Last reply
          0
          • C C Scharbe

            Try this: class Rue { public int Id; public string Nom; public override string ToString() { return Nom; } };

            B Offline
            B Offline
            baranils
            wrote on last edited by
            #5

            Thank you It works indeed ! But I really do not understand why and how ? I'm old to C but new to csharp and oop

            C 1 Reply Last reply
            0
            • B baranils

              Thank you It works indeed ! But I really do not understand why and how ? I'm old to C but new to csharp and oop

              C Offline
              C Offline
              C Scharbe
              wrote on last edited by
              #6

              The combobox run ToString() for each item. If you have a class, that does not override the ToString() method, it returns the name of the class- type. I guess you don't need to run cmb_rues.DisplayMember = ... ??

              B 1 Reply Last reply
              0
              • C C Scharbe

                The combobox run ToString() for each item. If you have a class, that does not override the ToString() method, it returns the name of the class- type. I guess you don't need to run cmb_rues.DisplayMember = ... ??

                B Offline
                B Offline
                baranils
                wrote on last edited by
                #7

                I understand better now Thank you !

                1 Reply Last reply
                0
                • B baranils

                  Hello I'm trying to bind a list to a combobow but without effective result class Rue { public int Id; public string Nom; }; List<Rue> Rues = new List<Rue>(); Then I'm trying to fill and bind that list to a Combo Rues.Clear(); readRues("1000",Rues); cmb_rues.DataSource = Rues; cmb_rues.DisplayMember = Rues[0].Nom; The Combo displays things but not what I expect It shows App.Form+Rue App.Form+Rue App.Form+Rue etc.. Thanks for any help !

                  C Offline
                  C Offline
                  CodingYoshi
                  wrote on last edited by
                  #8

                  Create properties for your members and let the display member be a property. It should work. Let me know if it doesn't.

                  B 1 Reply Last reply
                  0
                  • C CodingYoshi

                    Create properties for your members and let the display member be a property. It should work. Let me know if it doesn't.

                    B Offline
                    B Offline
                    baranils
                    wrote on last edited by
                    #9

                    Thank you It works !

                    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