winforms - c# combobox binding to list of objects -


quick question, possible bind combobox list of objects, have selectedvalue property point object, not property of object.

i ask because have business objects have references other objects - such 'year' object. year object may need switched out year object.

only solution can come have class single property, in case pointing year object. bind combobox list of these , set both display , value members single property.

but doing 'lookups' have seems bit of pain??

marlon

if set valuemember null selected value object, not property:

{     public class testobject     {         public string name { get; set; }         public int value { get; set; }     }     public partial class form1 : form     {         private system.windows.forms.combobox combobox1;          public form1()         {             this.combobox1 = new system.windows.forms.combobox();             this.suspendlayout();             //              // combobox1             //              this.combobox1.formattingenabled = true;             this.combobox1.location = new system.drawing.point(23, 13);             this.combobox1.name = "combobox1";             this.combobox1.size = new system.drawing.size(121, 21);             this.combobox1.tabindex = 0;             this.combobox1.selectedvaluechanged += new system.eventhandler(this.combobox1_selectedvaluechanged);             //              // form1             //              this.autoscaledimensions = new system.drawing.sizef(6f, 13f);             this.autoscalemode = system.windows.forms.autoscalemode.font;             this.clientsize = new system.drawing.size(284, 262);             this.controls.add(this.combobox1);             this.name = "form1";             this.text = "form1";             this.resumelayout(false);              bindinglist<testobject> objects = new bindinglist<testobject>();             (int = 0; < 10; i++)             {                 objects.add(new testobject() { name = "object " + i.tostring(), value = });             }             combobox1.valuemember = null;             combobox1.displaymember = "name";             combobox1.datasource = objects;         }          private void combobox1_selectedvaluechanged(object sender, eventargs e)         {             if (combobox1.selectedvalue != null)             {                 testobject current = (testobject)combobox1.selectedvalue;                 messagebox.show(current.value.tostring());             }         }     } } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -