c# - Adding a value to DataGrid ComboBox during runtime. The value should not be part of combobox list -


i'm trying add value combo box in data grid view table, during run time. first have added items combo box. during run time trying add data datatable data grid. if data not part of combo box item, not accepting. want accept value , display in grid, though not part of combobox. see code sample, trying below:

namespace windowsformsapplication1 {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }         private void form1_load(object sender, eventargs e)         {             datatable dt = new datatable();              dt.columns.add("fieldname");             dt.columns.add("value");             dt.columns.add("flags");             dt.rows.add("field1", "value1", "#a");             dt.rows.add("field2", "value2", "#b");             dt.rows.add("field3", "value3", "#c");             dt.rows.add("field4", "value4", "#d");             dt.rows.add("field5", "value5", "#e");              foreach (datarow dr in dt.rows)             {                 string fieldname = convert.tostring(dr["fieldname"]);                 string value = convert.tostring(dr["value"].tostring());                 string flags = convert.tostring(dr["flags"].tostring());                 datagridview1.rows.add(fieldname, value, flags);             }              combobox cb = new combobox();             addlistcombobox(ref cb);             ((datagridviewcomboboxcolumn)datagridview1.columns["flags"]).datasource = cb.items;          }          datatable csvdatatable = new datatable();         public datatable defaultflagsdatatable()         {             //create default flags datatable             if (csvdatatable.rows.count <= 0)             {                 csvdatatable.columns.add("name", typeof(string));                 csvdatatable.columns.add("description", typeof(string));                 csvdatatable.columns.add("obsolete", typeof(bool));                 csvdatatable.columns.add("color", typeof(string));             }              //add default flags datatable             csvdatatable.rows.add("#a", "asset data", false, 13168840);             csvdatatable.rows.add("#b", "system default", false, 15780518);             csvdatatable.rows.add("#c", "data geoplan", false, 65280);             csvdatatable.rows.add("#d", "model import", false, 3981040);             csvdatatable.rows.add("#e", "system calculated", false, 3981040);             //csvdatatable.rows.add("#f", "csv import", false, 33023);              return csvdatatable;         }          public void addlistcombobox(ref combobox cb)         {             defaultflagsdatatable();             foreach (datarow dr in csvdatatable.rows)             {                 string flags = dr["name"].tostring();                 cb.items.add(flags);             }         }       } } 

assuming combobox value bound datagrid, cannot work. items in combobox have contain possible values in database field. akin enumeration. in such cases have separate 'options' table in database listing options (usually in id, description format) , impose foreign key restriction on main table. instead of manually creating list in code, read contents of options table datatable , make datasource of combobox.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -