removing item from comboBox if it matches a cetain cell in dataGridView in VB.net -
i have list of names (employees) in combobox use add datagridview. i'm trying make once employee in database , displayed in datagrid, name removed combobox not appear available add datagrid. code i've written task looks should work, doesn't.
public class main
public techphones new phonesdataset.techcompanyphonesdatatable public techphonestableadapter new phonesdatasettableadapters.techcompanyphonestableadapter public employees new phonesdataset.employeesdatatable public emptableadapter new phonesdatasettableadapters.employeestableadapter private sub form1_load(byval sender system.object, byval e system.eventargs) handles mybase.load, mybase.activated loadform() end sub private sub cboemployee_selectedindexchanged(byval sender system.object, byval e system.eventargs) handles cboemployee.selectedindexchanged lbloffice.text = "office: " + cboemployee.selecteditem("office") lblphone.text = cboemployee.selecteditem("phone") lblempid.text = cboemployee.selecteditem("empid") end sub private sub btnadd_click(byval sender system.object, byval e system.eventargs) handles btnadd.click, datagridview1.celldoubleclick dim addemp new addemployee(cboemployee.selecteditem("employee"), cboemployee.selecteditem("office"), cboemployee.selecteditem("phone"), _ cboemployee.selecteditem("officeid"), cboemployee.selecteditem("empid"), cboemployee.selecteditem("contract"), cboemployee.selecteditem("returned"), _ cboemployee.selecteditem("loaner")) addemp.showdialog() end sub private sub datagridview1_cellcontentclick(byval sender system.object, byval e system.windows.forms.datagridviewcelleventargs) handles datagridview1.cellcontentclick dim name string = datagridview1.rows(datagridview1.currentcell.rowindex).cells(2).value dim employee string index integer = 0 cboemployee.items.count - 1 step 1 if cboemployee.items(index)("employee") = name employee = cboemployee.items(index)("employee") cboemployee.selectedindex = index end if next end sub private sub btndelete_click(byval sender system.object, byval e system.eventargs) handles btndelete.click techphonestableadapter.deletequery((datagridview1.rows(datagridview1.currentcell.rowindex).cells(1).value)) loadform() end sub private sub loadform() techphones = techphonestableadapter.getdata() datagridview1.datasource = techphones employees = emptableadapter.getdata() cboemployee.datasource = employees cboemployee.displaymember = ("employee") cboemployee.valuemember = ("empid") lbloffice.text = "office: " + cboemployee.selecteditem("office") lblphone.text = cboemployee.selecteditem("phone") lblempid.text = cboemployee.selecteditem("empid") cboemployee.focus() cboemployee.selectall() lblrowcount.text = "there " + datagridview1.rowcount.tostring + " phones accounted for." ' code below supposed check every row in data grid against employee list in ' cboemployees , remove list when match datagridview1 indexdgv integer = 0 .rows.count - 1 step 1 cboemployee.items.remove(.rows(indexdgv).cells(2).value) next end cboemployee.refresh() end sub
end class
i've tried minor variation:
datagridview1 indexdgv integer = 0 .rows.count - 1 step 1 indexcbo integer = cboemployee.items.count - 1 0 step -1 if .rows(indexdgv).cells(2).value = cboemployee.items(indexcbo)("employee") cboemployee.items.removeat(indexcbo) end if next next end
you don't need iterate through combobox items. .remove (string) work, , if item want remove not there, ignores command.
with datagridview1 indexdgv integer = 0 .rows.count - 1 step 1 cboemployee.items.remove(.rows(indexdgv).cells(2).value) next end
Comments
Post a Comment