c# - How to change the text of a comboBox cell of a dynamically added DataGridViewComboBoxColumn? -
c# - How to change the text of a comboBox cell of a dynamically added DataGridViewComboBoxColumn? -
i still little new c# using winforms , have datagridview connected datasource , beingness populated correctly.
i have added comboboxcolumn @ run time. comboboxcolumn beingness connected datasource, displaymember , valuemember set, headertext set , column added datagrid. connection works fine , when user clicks combobox populated expected.
when user completes new row in grid select item in combobox , whole row updated database when done.
my question is: when datagrid opened 1 time again or redrawn populated due datasource property, how combobox cell display value selected instead of blank box. know code of getting value db , set in value. ideal if set variable in display of combobox. maintain in mind combobox still info bound user edit value if wishes?
i know in normal combobox command should set .text property. datagridviewcombobox not have same property.
here code actual info connection , adding of comboboxcolumn:
public void addcomboboxcolumn(datagridview datagridname, datatable table, string headertext, int columnindex) { datagridviewcomboboxcolumn column = new datagridviewcomboboxcolumn(); getdisplayandvaluemembers(table, headertext); //calls method gets datasource, displaymember, valuemember depending on column column.datasource = tableref; //sets datasource table referenced column column.displaymember = displaymember; //sets displaymember column.valuemember = valuemember; //sets valuemember column.headertext = headertext; //changes headertext displayed text if (newcolumnindex == 0) datagridname.columns.add(column); //added end of datagrid else { datagridname.columns.removeat(columnindex); datagridname.columns.insert(newcolumnindex, column); //added in specific index if needed } }
this shows dataconnection of drop down. there many methods beingness used big amount of code. not problem works fine. don't know how go issue of showing selected item text of combobox?
it sounds looking datapropertyname
property of comboboxcolumn
. property creates binding between datasource
of datagridview
, selected value of comboboxcolumn
.
for example, have list of products displayed in combo boxes. have productid in datagridview
datasource
. this:
// there orders info table coming db includes product id column datagridview1.datasource = orders; // set column same, displaymember , valuemember datagridviewcomboboxcolumn column = new datagridviewcomboboxcolumn(); getdisplayandvaluemembers(table, headertext); column.datasource = tableref; //sets datasource table referenced column column.displaymember = displaymember; //sets displaymember column.valuemember = valuemember; //sets valuemember column.headertext = headertext; //changes headertext displayed text //now set datapropertyname column.datapropertyname = "productid"; // name of column or property grid datasource datagridview1.columns.add(column);
with datapropertyname
set have databinding between comboboxcolumn
, datagridview
.
if underlying info source absolutely cannot have value of combo box property in need handle of saving , value setting of comboboxcolumn
within custom code.
to set selected value of datagridviewcomboboxcell
select cell , set value
property.
datagridview1.rows["rowname"].cells["columnname"].value = valuefromdb;
c# winforms datagridview datagridviewcombobox
Comments
Post a Comment