c# - Remove Specific Value from DropDown -
i beginner dot.net, can 1 suggest me how remove specific value in fetched dropdown list.
here why mentioning fetched means dropdownlist generated or fetched table using sql.
sqlquery=select distinct rtrim(ltrim(c.cust_name)) cust_name table1 inner join table2 b on a.cust_code = b.cust_code sqlcommand cmd = new sqlcommand(sqlquery, conn); sqldatareader sr = cmd.executereader(); while (sr.read()) { { cmb_cust.items.add(sr["cust_name"].tostring()); } } foreach (listitem li in cmb_cust.items) { { if (li.value == "value1") cmb_cust.items.remove(li); } }
if process above statement facing collection modified enumeration operation may not execute, can other solution swaping list temp list , process operation. don`t need value1 after fetching sql.
even better removing item afterwards, don't add in first place.
while (sr.read()) { var txt = sr["cust_name"].tostring(); if (txt != "value1") cmb_cust.items.add(txt); }
or can exclude in sql query.
select tbl1.cust_name ( select distinct rtrim(ltrim(c.cust_name)) cust_name table1 inner join table2 b on a.cust_code = b.cust_code ) tbl1 tbl1.cust_name != 'value1'
the reason getting exception can not remove items collection while being enumerated. if want remove item should write:
cmb_cust.items.remove("value1");
Comments
Post a Comment