c# - Bound DataGridView: the DataGridViewComboBoxColumn changes to a different value when I click outside it -


i hope can me out obscure problem has me absolutely flummoxed.

i wish set datagridview, allows user select option datagridviewcomboboxcolumn, , object datasource datagridview, updated object user pick in combobox.

[nb wish datagridviewcomboboxcolumn show more single property in dropdown, hence why using datatable datagridviewcomboboxcolumn datasource. in other words, want them see description combination of other properties concatenated together.]

my code works, when click outside combobox cell, value gets automatically set (it gets set first item in bindinglist). cannot stick value user selected. wrong object instance gets assigned datagridview's datasource.

i post pic, don't have enough rep.

so question is, why datagridviewcomboboxcolumn switch first item in datasource (a datatable) when click outside cell. how can keep option selected, in cell. absolutely baffled.

i hope it's okay post code stackoverflow website without annoying everyone. i've tried create suitably generic example other souls trying find out how select , assign object property of object, using datagridviewcomboboxcolumn. of use else. i've tried make relatively easy recreate, should need solve kind of problem.

i've tried manner of things, including using list datasource of datagridviewcomboboxcolumn, doing things currentcelldirtystatechanged event - no avail.

here's 2 classes:

    public class carpartchoice     {         public string name { get; set; }         public int value { get; set; }         public string comment {get; set;}          public carpartchoice(string name, int value, string comment)         {             name = name;             value = value;             comment = comment;         }     }      public class car     {         public string manufacturer { get; set; }         public string model { get; set; }         public carpartchoice wheelchoice {get; set;}          public car(string maker, string model, carpartchoice wheel)         {             manufacturer = maker;             model = model;             wheelchoice = wheel;         }          public car(string maker, string model)         {             manufacturer = maker;             model = model;         }     }      public static class globalvariables     {         public static bindinglist<carpartchoice> globalchoicelist { get; set; }         public static bindinglist<car> globalcarslist { get; set; }     } 

and here's me creating datagridview:

        private void form1_load(object sender, eventargs e)         {             //setup wheel choices selected datagridviewcomboboxcolumn.             carpartchoice mywheelchoice = new carpartchoice("chrome", 19, "this chromes wheels option.");             carpartchoice mywheelchoice2 = new carpartchoice("hubcaps", 16, "this nasty plastic hubcaps option.");             bindinglist<carpartchoice> tempblchoice = new bindinglist<carpartchoice>();             tempblchoice.add(mywheelchoice);             tempblchoice.add(mywheelchoice2);             globalvariables.globalchoicelist = tempblchoice;              //setup cars populate datagridview.             car car1 = new car("vauxhall", "astra");             car car2 = new car("mercedes", "s-class");             bindinglist<car> templistcars = new bindinglist<car>();             templistcars.add(car1);             templistcars.add(car2);             globalvariables.globalcarslist = templistcars;              datagridview1.autogeneratecolumns = false;             datagridview1.currentcelldirtystatechanged += new eventhandler(datagridview1_currentcelldirtystatechanged);              // set 2 datagridviewtextbox columns, 1 show manufacturer , other show model.             datagridviewtextboxcolumn manufacturer_col = new datagridviewtextboxcolumn();             manufacturer_col.datapropertyname = "manufacturer";             manufacturer_col.name = "manufacturer";             manufacturer_col.headertext = "manufacturer";             datagridviewtextboxcolumn model_col = new datagridviewtextboxcolumn();             model_col.datapropertyname = "model";             model_col.name = "model";             model_col.headertext = "model";              // create datatable hold wheel options available user choose from. dt datasource              //  ...combobox column             datatable wheelchoices = new datatable();             datacolumn choice = new datacolumn("choice", typeof(carpartchoice));             datacolumn choicedescription = new datacolumn("description", typeof(string));             wheelchoices.columns.add(choice);             wheelchoices.columns.add(choicedescription);             foreach (carpartchoice wheelchoice in globalvariables.globalchoicelist)             {                 wheelchoices.rows.add(wheelchoice, wheelchoice.name + " - " + wheelchoice.value.tostring() + " - " + wheelchoice.comment);             }              // create combobox column, populated wheel options user can pick one.             datagridviewcomboboxcolumn wheeloption_col = new datagridviewcomboboxcolumn();             wheeloption_col.datapropertyname = "wheelchoice";             wheeloption_col.name = "wheelchoice";             wheeloption_col.datasource = wheelchoices;             wheeloption_col.valuemember = "choice";             wheeloption_col.displaymember = "description";             wheeloption_col.valuetype = typeof(carpartchoice);              // add columns , set datasource dgv.             datagridview1.columns.add(manufacturer_col);             datagridview1.columns.add(model_col);             datagridview1.columns.add(wheeloption_col);             datagridview1.datasource = globalvariables.globalcarslist;         } 

update: have tried use "bindingsource" object , setting datasource of bindingsource datatable. made no difference. i've tried getting car implement "inotifypropertychanged" interface , didn't make difference.

the 1 thing if i've noticed car item in datagridview wheelchoice property updated. car objects indeed updated value i've selected combobox. think display issue , datagridviewcomboboxcolumn isn't populated correct value. still baffled.

thanks left comments. found answer in end.

the answer me define overridden "tostring()" method "carpartchoice" class (i.e. class used provide items looked-up clicking on comboboxcolumn). guess having trouble formatting cell nice string when moved control away it.

so, ever wants in future, here's full example of how might use datagridview update list of objects of class, , using datagridviewcomboboxcolumn provide user choice of objects, , selected object (which chose dropdown), populated list (to precise: in field of object in list of type of object gets selected user in dropdown). yes, know that's horrible sentence.

here's complete code accomplish task (i have thought people want do).

kindest regards all.

    public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private void form1_load(object sender, eventargs e)         {             //setup wheel choices selected datagridviewcomboboxcolumn.             carpartchoice mywheelchoice = new carpartchoice("chrome", 19, "this chromes wheels option.");             carpartchoice mywheelchoice2 = new carpartchoice("hubcaps", 16, "this nasty plastic hubcaps option.");             carpartchoice mywheelchoice3 = new carpartchoice("iron", 15, "these metal wheels.");             carpartchoice mywheelchoice4 = new carpartchoice("spoked", 15, "this fancy classic hubcaps option.");             carpartchoice mywheelchoice5 = new carpartchoice("solid", 13, "this wheels has no spokes or holes.");             carpartchoice mywheelchoice6 = new carpartchoice("spacehubcaps", 17, "newly developed space hubcaps.");              bindinglist<carpartchoice> tempblchoice = new bindinglist<carpartchoice>();             tempblchoice.add(mywheelchoice);             tempblchoice.add(mywheelchoice2);             tempblchoice.add(mywheelchoice3);             tempblchoice.add(mywheelchoice4);             tempblchoice.add(mywheelchoice5);             tempblchoice.add(mywheelchoice6);              globalvariables.globalchoicelist = tempblchoice;              //setup cars populate datagridview.             car car1 = new car("vauxhall", "astra");             car car2 = new car("mercedes", "s-class");             bindinglist<car> templistcars = new bindinglist<car>();             templistcars.add(car1);             templistcars.add(car2);             globalvariables.globalcarslist = templistcars;              datagridview1.autogeneratecolumns = false;             datagridview1.currentcelldirtystatechanged += new eventhandler(datagridview1_currentcelldirtystatechanged);               // set 2 datagridviewtextbox columns, 1 show manufacturer , other show model.             datagridviewtextboxcolumn manufacturer_col = new datagridviewtextboxcolumn();             manufacturer_col.datapropertyname = "manufacturer";             manufacturer_col.name = "manufacturer";             manufacturer_col.headertext = "manufacturer";             datagridviewtextboxcolumn model_col = new datagridviewtextboxcolumn();             model_col.datapropertyname = "model";             model_col.name = "model";             model_col.headertext = "model";              // create datatable hold wheel options available user choose from. dt datasource              //  ...combobox column             datatable wheelchoices = new datatable();             datacolumn choice = new datacolumn("choice", typeof(carpartchoice));             datacolumn choicedescription = new datacolumn("description", typeof(string));             wheelchoices.columns.add(choice);             wheelchoices.columns.add(choicedescription);              foreach (carpartchoice wheelchoice in globalvariables.globalchoicelist)             {                 wheelchoices.rows.add(wheelchoice, wheelchoice.name + " - " + wheelchoice.value.tostring() + " - " + wheelchoice.comment);             }              // create combobox column, populated wheel options user can pick one.             datagridviewcomboboxcolumn wheeloption_col = new datagridviewcomboboxcolumn();             wheeloption_col.datapropertyname = "wheelchoice";             wheeloption_col.datasource = wheelchoices;             wheeloption_col.valuemember = "choice";             wheeloption_col.displaymember = "description";             wheeloption_col.valuetype = typeof(carpartchoice);              // add columns , set datasource dgv.             datagridview1.columns.add(manufacturer_col);             datagridview1.columns.add(model_col);             datagridview1.columns.add(wheeloption_col);             datagridview1.datasource = globalvariables.globalcarslist;         }          void datagridview1_currentcelldirtystatechanged(object sender, eventargs e)         {             var grid = sender datagridview;             if (grid.iscurrentcelldirty)                 grid.commitedit(datagridviewdataerrorcontexts.commit);         }     }      public class carpartchoice     {         public string name { get; set; }         public int value { get; set; }         public string comment { get; set; }          public carpartchoice(string name, int value, string comment)         {             name = name;             value = value;             comment = comment;         }          public override string tostring()         {             return name.tostring() + " - " + value.tostring() + " - " + comment.tostring();         }     }      public class car     {         public string manufacturer { get; set; }         public string model {get; set; }         public carpartchoice wheelchoice { get; set; }           public car(string maker, string model, carpartchoice wheel)         {             manufacturer = maker;             model = model;             wheelchoice = wheel;         }          public car(string maker, string model)         {             manufacturer = maker;             model = model;         }     }      public static class globalvariables     {         public static bindinglist<carpartchoice> globalchoicelist { get; set; }         public static bindinglist<car> globalcarslist { get; set; }     } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -