I have a combobox I need to display some data in from a Database, but there are duplicates and I don't know how to select the distinct ones.

Any help?

Recommended Answers

All 4 Replies

Have you tried doing SELECT DISTINCT ? i've had this proplem in the past and i think i used SELECT DISTINCT.

Yep, SELECT DISTINCT will provide you with a list of the unique values in the table. Records that are in the table twice or more will only be in the resulting dataset once.

Another option depending on how you get the data is to use Linq and call distinct on the object that holds the data. If it's something like a List<string> named lst_strData, you can call lst_strData.Distinct();

or pseudo-code

// GetDataFromSource would return a List<string> or array, etc.
List<string> lst_strData = GetDataFromSource().Distinct().ToList();

Well I found the solution but it isn't using "SELECT DISTINCT" which I had already tried...

For some reason:

DataView view = autoDealershipDataSet.tblCars.DefaultView;
            DataTable distinctTable = view.ToTable("DistinctMakeTable", true, "Make");

            this.tblCarsBindingSource.DataSource = distinctTable;

You can't use the Filter property of a binding source to select distinct items, there is a crappy work-around.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.