c# - Remove selected item in combobox1 from the combobox2. (Comboboxes are bound from database.) -
i have 2 comboboxes named cbteam1
, cbteam2
(winform & c#) both bound same database table.
if person selects team cbteam1
, want selected team not displayed in cbteam2
.
private void bindcombobox() { if (con.state == connectionstate.closed) { con.open(); } string queryteam1 = "select * teams order team_name"; sqlcommand cmd = new sqlcommand(queryteam1, con); adapter = new sqldataadapter(cmd); adapter.fill(ds, "teams"); this.cboxteam1.selectedindexchanged -= new eventhandler(this.cboxteam1_selectedindexchanged); cboxteam1.datasource = ds.tables["teams"]; //if(cboxteam1.selectedindex cboxteam1.displaymember = "team_name"; cboxteam1.selectedindex = -1; cboxteam1.valuemember = "team_id"; this.cboxteam1.selectedindexchanged += new eventhandler(this.cboxteam1_selectedindexchanged); }
and here code cboxteam2
event handler cboxteam2_selectedindexchanged
private void cboxteam2_selectedindexchanged(object sender, eventargs e) { if (cboxteam1.selectedindex == cboxteam2.selectedindex) { messagebox.show("you selected " + cboxteam2.text); } team2_id = int32.parse(cboxteam2.selectedvalue.tostring()); }
for example cboxteam1
displaye 3 values i.e england, india, austrailia. if select india, after selection india shold not display in cboxteam2
combobox
hey have made sample you.sample have 2 combo box , bind same data source.and when select combobox1
firstly check list if exist bind combobox 2
except matched item.
protected list<string> lst { { list<string> lst = new list<string>(); lst.add("1"); lst.add("2"); lst.add("3"); return lst; } } private void form1_load(object sender, eventargs e) { combobox1.datasource = lst; combobox2.datasource = lst; } private void combobox1_selectedindexchanged(object sender, eventargs e) { if (lst.contains(combobox1.selecteditem.tostring())) { combobox2.datasource = lst.select(q => q.tostring(cultureinfo.invariantculture)).where(q => q.tostring() != combobox1.selecteditem).tolist(); } }
Comments
Post a Comment