问题
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