c# - DataGridView linked to DataTable with Combobox column based on enum -
having spent lot of yesterday searching on 1 have give up.
i have datagridview linked datatable. 1 of columns enum , want show combobox column.
i found link create drop down list options enum in datagridview has answer of using following...
datagridviewcomboboxcolumn col = new datagridviewcomboboxcolumn(); col.name = "my enum column"; col.datasource = enum.getvalues(typeof(myenum)); col.valuetype = typeof(myenum); datagridview1.columns.add(col);
i've tried when user creates new record, chooses option drop down (the correct options show) moves off field message "datagridviewcomboboxcel value not valid". i've found solutions in searching talk how trap error, nothing (thus hiding error) want solve not hide it. if user ok's message repeat 2 times.
i've seen solutions loop through values in enum , create datatable containing int , string each one, using datatable datasource on combo. i've used datatable source combobox in past when working back-end mssql database.
another variant of loop through , write straight combo such this...
foreach (myenum bar in myenum.getvalues(typeof(myenum))) { string barname = myenum.getname(typeof(myenum), bar); mycombocolumn.items.add(barname); }
such in question in link. how can add enum values combobox
my question: can made work using enum.getvalues(typeof(myenum)); method? datatable method seems long winded. looping using mycombocolumn.items.add(barname); long winded , result in string version of enum being recorded in datatable not integer (and rather integer).
i can't find examples of enum.getvalues(typeof(myenum)) method grid linked datatable. when search on come across other methods.
i think problem lie in data type on underlying table column. i've tried integer, string , i've tried not defining it. can't think else try on type.
here simplified code. (dvg datagridview on form).
enum enginetype { none = 0, enginetype1 = 1, enginetype2 = 2 } public partial class myclass : form { datatable dttbl; public myclass() { initializecomponent(); createtablestructure(); } private void createtablestructure() { dgv.autogeneratecolumns = false; dgv.datasource = dttbl; dttbl = new datatable(); dttbl.columns.add(new datacolumn("name", system.type.gettype("system.string"))); datagridviewtextboxcolumn namecol = new datagridviewtextboxcolumn(); namecol.datapropertyname = "name"; namecol.headertext = "name"; dgv.columns.add(namecol); dttbl.columns.add(new datacolumn("engine", system.type.gettype("system.int32"))); datagridviewcomboboxcolumn enginecol = new datagridviewcomboboxcolumn(); enginecol.datapropertyname = "engine"; enginecol.headertext = "engine"; //enginecol.datasource = enginetype.getvalues(typeof(enginetype)); foreach (enginetype engine in enginetype.getvalues(typeof(enginetype))) { string enginename = enginetype.getname(typeof(enginetype), engine); enginecol.items.add(enginename); } dgv.columns.add(enginecol); } }
i had problem myself, here how fixed it:
datagridviewcomboboxcolumn col = new datagridviewcomboboxcolumn(); col.valuetype = typeof(myenum); col.valuemember = "value"; col.displaymember = "display"; colelementtyp.datasource = new myenum[] { myenum.firstenumvalue, myenum.secondenumvalue } .select(value => new { display = value.tostring(), value = value }) .tolist();
the datagridviewcomboboxcell has bound enum or integer. guess column of datatable integer, should work.
edit:
this should work dynamic:
system.array enumarray = enum.getvalues(typeof(myenum)); list<myenum> lst = enumarray.oftype<myenum>().tolist(); datagridviewcomboboxcolumn col = new datagridviewcomboboxcolumn(); col.valuetype = typeof(myenum); col.valuemember = "value"; col.displaymember = "display"; colelementtyp.datasource = lst .select(value => new { display = value.tostring(), value = value }) .tolist();
shorter:
datagridviewcomboboxcolumn col = new datagridviewcomboboxcolumn(); col.valuetype = typeof(myenum); col.valuemember = "value"; col.displaymember = "display"; colelementtyp.datasource = enum.getvalues(typeof(myenum)).oftype<myenum>().tolist() .select(value => new { display = value.tostring(), value = value }) .tolist();
Comments
Post a Comment