winforms - Combo box is not displaying default value in DataGridView in C# -
i'm trying allow user select value datagridviewcomboboxcolumn
each new row. i've bound combobox
in gridview
database, whenever enter new row see datagridviewcomboboxcolumn
no initial value. have set value first.
how can let default value appear in datagridviewcomboboxcolumn
whenever enter new row in datagridview
?
this how i'm binding datagridview
combobox
database :
bindingsource1.datasource = itembal.gettable(); datagridview1.datasource = bindingsource1; itemname.datasource = bindingsource1; //where itemname instance ofdatagridviewcomboboxcolumn itemname.displaymember = "name"; itemname.valuemember = "itemid";
you can use defaultcellstyle.nullvalue
, defaultcellstyle.datasourcenullvalue
properties of combo box column.
there quite comprehensive msdn article on here.
i've given code example below:
// let's have list of cars want add datagridview list<car> cars = new list<car>(); cars.add(new car() { index = 0, make = "ford" }); cars.add(new car() { index = 1, make = "chevvy" }); cars.add(new car() { index = 2, make = "toyota" }); // create column, setting various properties datagridviewcomboboxcolumn col = new datagridviewcomboboxcolumn(); col.datasource = cars; col.name = "cars"; col.displaymember = "make"; col.headertext = "car"; col.valuemember = "index"; col.datapropertyname = "car"; // standard properties combobox column set these two: col.defaultcellstyle.nullvalue = cars[0].make; col.defaultcellstyle.datasourcenullvalue = cars[0].index; datagridview1.columns.add(col); datagridview1.datasource = dt;
one thing note code above setting datapropertyname
allow binding property on datagridview's datasource.
if not doing need add additional logic when accessing combo box column when users have selected nothing, since value of property null (the nullvalue not set value actual cell, shows default value).
Comments
Post a Comment