how to filter out duplicate values?

I have a DataTable with the following schema and sample data:


ProductID  |  LocationID  | ProductName  |  Quantity  |  SellingPrice
---------------------------------------------------------------------
     1     |       1      |     xyz      |     10     |    5.0000
     1     |       2      |     xyz      |     15     |    5.0000
     2     |       1      |     abc      |     10     |    7.0000
     3     |       1      |     def      |     30     |    9.5000
     3     |       2      |     def      |     60     |    9.5000


When I bind this datasource to a combobox (DisplayMember = ProductName, ValueMember = ProductID), some products are repeated. How can I only show distinct results in the combobox


Answer this question

how to filter out duplicate values?

  • Catalin Criveteanu

    I'll start by assuming that your data doesn't appear like this in the table itself. With that said, it looks like you want to use this DataTable in more than one place so you're using an inner join SQL statement combining 2 tables

    If that's true, simply make 2 SQL statements. If you're using SQL Server, you can do that with a single stored procedure.

    On the other hand if this <u>IS</u> coming from a single table, you have a problem with the first rule of data normalization and you should fix that first.

  • John Long

    Your advice is well taken (and appreciated). I guess I should give some more detail as to what I want to do. I thought this was irrelevant when I made the initial posts but I guess I was mistaken.

    I am making an accounting application where (apart from a lot of other things) the user can enter sales invoices. The DataTable I posted above is used to verify that any product entered in the invoice does not fall below stock (location is selected by the user) and to give the user a warning if it is sold below cost. For this purpose, the ProductName column is not required. But I added it so that I could use the same information to populate the combobox (which is actually in the DataGrid as a DataGridComboBoxColumn). I thought I could filter out duplicate products through the DataView or something. I don't think that is possible, is it

    Oh yeah, and I <em>am</em> using SQL Server.

    Now, as per your advice, if I use a DataReader to populate the DataTable, will this not affect the performance of my application  Let's say I have about 300 products and 2 location where the products are stored. So that is 300 iterations to populate the DataTable for the combobox and another 300 X 2 = 600 iterations for the DataTable to store the stock and price information. A total of 900 iterations in the constructor (or FormLoad event).

  • kneeride

    You are right in your assumptions. The data comes from an SQL query that joins two tables and I want to use this DataTable in two different places.

    I don't know if I have understood what you mean. Are you suggesting that I do not join the tables in the SQL query, rather retrieve two tables as they are and then join them in the DataSet via a DataRelation

  • fewfewfew

    Again, I assume you need this information in the format that it is currently in for something else so go ahead and do that. What I also see is that you are trying to use this same set of data (maybe since it's already there) for something that it shouldn't be used for.

    I completely agree with making as few round trips to the data source as possible and you question intonates that concern. It is possible to make a single trip to a SQL Server (for instance) and grab this DataTable and a 2nd one made just for your drop down in one quick visit.

    If you are using SQL Server (I don't know that you are), you simply place 2 valid SQL statements that each return the set of data you want in a single stored procedure. Ensure you define the ends of those SQL statements with a semi-colon ";" so SQL Server knows they are 2 different calls.

    Then, retrieve your data from this stored procedure just like any other stored procedure and what you will get is the first set of data. To move to the next set, use the ".NextResult" method on your SqlDataReader object to point to the 2nd set of data you defined in your stored procedure.

    I don't have all your information so I've made a lot of assumptions. I don't know how much it will help. Good luck anyway.

  • Andy Tischaefer - MSFT

    Thanks a lot for those links. And thanks for helping me resolve this issue :-)

    I don't know if it is appropriate for me to post this here, but I would like to draw your attention to another post [<post>7496</post>] that is related to the same stuff that I am doing.

  • Tominator2005

    Here are a couple of links I found useful while I was trying to figure this out for you:

    <a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconsettingupdatatabledatacolumnmappings.asp">Setting Up DataTable and DataColumn Mappings</a>

    <a href="http://www.dotnet247.com/247reference/msgs/28/143418.aspx">See David Sceppa's Explination near the bottom of this page</a>

  • LeCoop

    I will look into the SQLXML article right away. In the mean time, do you have any examples of how to use the DataAdapter and TableMappings to fill multiple DataTables  I tried to find some examples but all of the ones I found use a DataReader.
  • Ernesto Vasquez.

    As I understand it, if you use an inner-joined SQL statement to feed a DataTable, as far as the DataTable is concerned, it's a single table. Therefore, you won't have any Group-By functionality within the DataView and I think that's what you're looking for. On the other hand, if you add 2 DataTables to a single DataSet, you could use the DataRelation to create the grouping effect you're looking for but that’s what you’re already doing with the inner-joined SQL statement. I think that would take you in a much different direction. All that said, it's still my suggestion to use the single stored procedure with 2 SQL statements.

    As for using a DataReader as opposed to a DataTable, forget I said DataReader. In your case, you'll want to use a DataAdapter and TableMappings to fill multiple DataTables in the same DataSet at the same time.

    Here's another alternative using SQLXML that I just found:
    <a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnhcvb03/html/vb03e9.asp">Populating a Multi-table DataSet with SQLXML</a>

  • how to filter out duplicate values?