vb.net - SOLVED: Adding and Populating DataGridViewComboBoxcolumn to Bound Datagridview -
edited show correct solution inline in case searched others in future
i'm attempting allow datagridview allow user edit fields (for simplicity's sake, let's it's items, each of can have condition, allowable values taken second table [conditions]
e.g.
data structure (simplified)
items table
- id (primary key--not shown)
- itemnum
- qty
- conditionabbrev (e.g. "new", "used", "recert") -- used key item in conditions table below
conditions table
- conditionabbrev (e.g. "new", "used", "recert", etc.
- conditiondescription (lengthy description of condition)
desired appearance of grid:
my grid should like:
item # qty condition 123456 10 [ new v] <-- drop-down 234567 55 [ used v] 345678 99 [ new v] etc. the strategy:
i'm attempting set by:
binding grid first items table (grabbing first 3 columns contain actual values of each line of items table)
creating new datagridviewcomboboxcolumn ("condcombo") , binding allowable items conditions table it,
3. looping through grid , setting value of condcombo each row value of conditions rownot necessary it'll handled automatically dataproperty (see below)hiding conditions (text) column.
the problem:
i'm able column added , loaded conditions values, i'm utterly failing @ setting selected value match condition items table; furthermore, selections i'm making in combo being blanked moment tab or click cell.
here's code i've got far: hugely appreciated!
corrected code shown below:
sub setupgrid(byref mygrid datagridview, myconn sqlite.sqliteconnection) dim myadapter system.data.sqlite.sqlitedataadapter mygrid.virtualmode = true myadapter = new system.data.sqlite.sqliteadapter(_ "select id, itemnum, qty, condition items", myconn) myadapter.selectcommand.commandtype = commandtype.text ' fill main grid item data dim ds new dataset myadapter.fill(ds) mygrid.datasource = ds.tables(0) ' create , load combobox column dim cbocolumn new datagridviewcomboboxcolumn cbocolumn .datapropertyname = "conditionabbrev" .name = "condcombo" .headertext = "cond" ' bind combocolumn dim conditionsadapter system.data.sqlite.sqlitedataadapter dim condtable datatable using cmd new sqlite.sqlitecommand(_ "select conditionabbrev conditions", myconn) conditionsadapter.selectcommand = cmd conditionsapapter.fill(condtable) end using .datasource = condtable 'incorrect: .datapropertyname = "conditionabbrev" .datapropertyname = "condition" ' name of linked value in items table. changes combobox automatically reflected here. .valuemember = "conditionabbrev" .displaymember = .valuemember end ' set selected combo member same condition (text) field value: ' no longer needed (since work done automatically dataproperty linkage above ) ' each currow datagridviewrow in mygrid.rows() ' currow.cells("condcombo").value = _ ' currow.cells("condition").value ' next ' hide condition (text) field) mygrid.columns("condition").visible = false ' hide id field mygrid.columns("id").visible = false end sub
you're setting datapropertyname incorrectly. bind 1 list grid , 1 column. displaymember , valuemember names of columns/properties of items bound column. datapropertyname name of column/property of items bound grid. in case, looks me datapropertyname should set "condition".
Comments
Post a Comment