Set selectedindex for comboboxcell in datagridview vb.net -


i trying set selectedindex datagridviewcomboboxcell through cellvaluechanged event,i.e when change value in datagrid row, column should automatically changed. here's code.

 private sub datagridview1_cellvaluechanged(sender object, e system.windows.forms.datagridviewcelleventargs) handles datagridview1.cellvaluechanged            try                        if me.datagridview1.rows(e.rowindex).cells(2).value.tostring <> nothing                     me.datagridview1("unit_code", e.rowindex).value = "1"                      msgbox(me.datagridview1.rows(e.rowindex).cells(6).value)                 end if             else                 exit sub             end if          end if     catch ex exception         messagebox.show(ex.message, "message", messageboxbuttons.ok, messageboxicon.error)     end try  



datagridview bound dataset. searched many sites , did research, got above solution need set valuemember .value part. after doing gives
system.formatexception:datagridviewcomboboxcell value not valid.

please me.

[edit]
table structure
unit_code    descp
0                     -
1                     nos
2                     kgs
3                     gms

[edit 2]

 query = "select switch(qty=0,'2',qty<=rcvd_qty,'1',rcvd_qty=0,'2',qty>rcvd_qty,'3') status,item_type,catalogue,name,pd.rate,qty,pd.unit_code" _                 & " pur_det pd,itemhead th pd.item_code=th.itemcode , order_no=0 order catalogue"              adap1 = new oledbdataadapter(query, conn)             dim saldet new dataset             adap1.fill(saldet, "puritems")              me.datagridview1.datasource = saldet.tables(0)             datagridview1.columns.item(0).headertext = "status"             datagridview1.columns.item(0).width = 68             datagridview1.columns.item(1).headertext = "type"             datagridview1.columns.item(1).width = 60             datagridview1.columns.item(2).headertext = "catalogue"             datagridview1.columns.item(2).width = 140             datagridview1.columns.item(3).headertext = "description"             datagridview1.columns.item(3).width = 300             datagridview1.columns.item(4).headertext = "rate"             datagridview1.columns.item(4).width = 60             datagridview1.columns.item(5).headertext = "quantity"             datagridview1.columns.item(5).width = 84             datagridview1.columns.item(6).headertext = "unit" ' column removed below because had primary key values of table("unitmast").             datagridview1.columns.item(6).width = 70              adap1 = new oledbdataadapter("select * unitmast order unitcode asc", conn)             dim unitc new dataset             adap1.fill(unitc, "unitmast")              me.datagridview1.columns.removeat(6) 'here same added display member view actual value             dim uncol new datagridviewcomboboxcolumn             uncol                 .name = "unit_code"                 .datapropertyname = "unit_code"                 .datasource = unitc.tables(0)                 .valuemember = "unitcode"                 .displaymember = "desc"                 .headertext = "unit"                 .flatstyle = flatstyle.flat                 .dropdownwidth = 160                 .width = 70             end             me.datagridview1.columns.insert(6, uncol) 

a datagridviewcomboboxcell has no selectedindex or selectedvalue property.

you can set value directly this:

ctype(me.datagridview1("unit_code", e.rowindex), datagridviewcomboboxcell).value = "your value string" 

or using index of items collection: (this works if have not set valuemember , displaymember properties)

dim combo datagridviewcomboboxcell combo = ctype(me.datagridview1("unit_code", e.rowindex), datagridviewcomboboxcell) combo.value = combo.items('your index') 

you can check value nothing this:

if not me.datagridview1.rows(e.rowindex).cells(2).value nothing     'your code end if 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -