• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1953
  • Last Modified:

How can I hide duplicate field values in my GridView?

Hi all,

I have a GridView, which displays a result set, but many of the field values are duplciates (ie. it is selecting a list of all relationships for person "X", so person "X"'s name, city, address, etc. shows up in every single result). Is there a way that I can tell the GridView to "hide duplicates" in much the same way you can do this for reports in SS Reporting Services?

All help is greatly appreicated!
  • 4
  • 3
1 Solution
OddiCAuthor Commented:
EDIT: I shuold say: so person "X"'s name, city, address, etc. shows up in every single RECORD of the result set displayed in the GridView- I want to hide these duplicate values to make the gridview more readable)
Is Each row a complete duplicate, or is there at least 1 column in each row that has data that is not

OddiCAuthor Commented:
There are several other columns that are not duplicates.... I think I should just specify DISTINCT for the fields I do not want to display as dupllicates and make sure the JOINs are OUTER JOINs... this way I will have NULLs for the fields I am trying to hide duplicates for (and I can sort, etc. on the other fields).

Does this sound like a 'sound' approach? ((I was just hoping that maybe GridView had a property like Column.DisplayDuplicates or something that i could set in the app vs. changing the SP in the database...
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Well, is it possible to just hide those columns, or is there some data in those columns that you need to

OddiCAuthor Commented:
I want to display the first (DISTINCT) value, and hide this value (as it is duplicate) in all of the other records that have that duplicate value.

I *think* what I plan on doing will work...
That could work, however, if you allow the user to perform sorting on any field, then that will mess it up.

Additionally, I don't think DISTINCT is going to work for you the way you are expecting.  DISTINCT works
when every column for each row is exactly the same.  I don't think you can use DISTINCT on some
columns and not on others.

What you'll probably have to do, is loop through each row in the table, checking the vaue of the column that you don't want duplicates in, removing the values if they are duplicates.  Do all this before you
set the table as the DataGrid's DataSource.

Here's an example:

        'Create a table for this example:
        Dim table As New DataTable()

        'Add 5 rows, the first 3 rows have duplicated "ITEM"
        table.Rows.Add(1, "Item 1")
        table.Rows.Add(2, "Item 1")
        table.Rows.Add(3, "Item 1")
        table.Rows.Add(4, "Item 2")
        table.Rows.Add(5, "Item 3")

        'Get only the distinct rows:
        '    This will return ALL rows, because there are no duplicates.
        '    Every row is unique, because the value of column ID is unique
        'Dim distinctTable As DataTable = _
        'table.DefaultView.ToTable(True, New String() {"ID", "ITEM"})

        'Me.DataGridView1.DataSource = distinctTable

        'Stores value of previous ITEM
        Dim prevValue As String = String.Empty

        'Example:  Loop through each column and "hide" it
        '     if it is a duplicate of the previous row
        For Each row As DataRow In table.Rows
            If row.Item("ITEM").ToString() <> prevValue Then
                prevValue = row.Item("ITEM")
                row.Item("ITEM") = String.Empty
            End If

        Me.DataGridView1.DataSource = table

OddiCAuthor Commented:
That sounds like exactly what I need. Thank you friend!

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now