c# - Remove duplicate values in list box -
i have list box , bind table position , display column position. inserted 2 values w/ same position in table , list box displays 2 of same values inserted. so, can me display 1 of same value in list box? have code far:
private void listbox1_selectedindexchanged(object sender, eventargs e) { try { string select; select = listbox1.selectedvalue.tostring(); datagridview1.datasource = this.maindatabasedataset.tblposition.select("position '%" + select + "%'"); int numrows = datagridview1.rows.count; txtcount.text = convert.tostring(numrows); txtsearchcategory.text = select.tostring(); try { maindatabasedataset.tblcategorizationdatatable getcategorycommand1 = getcategorydata(this.txtsearchcategory.text.trim()); maindatabasedataset.tblcategorizationrow getcategorycommand2 = (maindatabasedataset.tblcategorizationrow)getcategorycommand1.rows[0]; this.txtsg.text = getcategorycommand2.salarygrade.tostring(); this.txtmales.text = getcategorycommand2.male.tostring(); this.txtfemales.text = getcategorycommand2.female.tostring(); } catch { messagebox.show("this position not saved yet!", "information".toupper(), messageboxbuttons.ok, messageboxicon.information, messageboxdefaultbutton.button1); txtsg.text = ""; txtmales.text = ""; txtfemales.text = ""; } } catch { return; } }
you can selecting distinct in linq. try data source:
datagridview1.datasource = this.maindatabasedataset .groupby(i => i.position) .select(i => i.first()) .where(i => i.position.contains(select));
include using system.linq;
namespace in project or reference it. let me know if works you.
Comments
Post a Comment