How to bind a list to a combobox
-
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 ComboRues.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 ! -
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 ComboRues.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 ! -
I think you need to write:
cmb_rues.DataSource = Rues; cmb_rues.DisplayMember = "Nom"; cmb_rues.ValueMember = "Id";
-
Hello I've try it too !! Same result BUT If I declare the ValueMember I got a message cannot bind to the new displaymember
-
Try this:
class Rue { public int Id; public string Nom; public override string ToString() { return Nom; } };
-
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
-
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 = ...
?? -
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 ComboRues.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 !Create properties for your members and let the display member be a property. It should work. Let me know if it doesn't.
-
Create properties for your members and let the display member be a property. It should work. Let me know if it doesn't.