Remove duplicates from combobox which is bind to dataset

試著忘記壹切 提交于 2019-12-24 01:14:51

问题


I have a xml file in my project. I am reading the file through the below code in to combobox cbProduct. The problem is that the cbProduct is displaying duplicate text values. How to make them distinct?

I have gone through some links but there way of approach is not related to dataset.
I implemented the below code:

DataSet ds = new DataSet();
ds.ReadXml(@"..\..\stock.xml");
cbProduct.DataSource = ds.Tables[0];
cbProduct.DisplayMember = "productname";

optional: If you have time it will be appreciable if you explain the process because I am new to .net or provide a link atleast to refer (not msdn).

Please Help.
Thanks in advance.


回答1:


Do this

DataSet ds = new DataSet();
ds.ReadXml(@"..\..\stock.xml");
DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "productname");
cbProduct.DataSource = dt;
cbProduct.DisplayMember = "productname";

Third code line creates a new table which will have distinct values based on productname column. For More read this

This code is here




回答2:


You can bring distinct values from database or you can get distinct values from c# data table into new c# data table and bind it to dropdown. How to select distinct value.



来源:https://stackoverflow.com/questions/12757138/remove-duplicates-from-combobox-which-is-bind-to-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!