Help with filtering datatable using VB.net

Hi,

I'm loading data on a grid from a datatable using the code below, How do you filter rows in the Grid to only display records if two or more values for a particular column are identical?

  Dim DV As New DataView(dta, "Receiver like " & "'" & C1Receiver.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)

        Dim FilteredDT As DataTable
        FilteredDT = DV.ToTable
        C1TrueDBGrid45.DataSource = FilteredDT

For example if the NSC column in the grid contains
SN            NSC
10411      TOYOTA
10411       HONDA
10411       TOYOTA
I want to press on a button and only display

SN           NSC
10411     TOYOTA
10411       TOYOTA

Thanks,
Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
I would do it at the database level, by adding a Count column that would be returned when you build the DataTable, but hidden in the grid. You could then filter your DataView on that column.
0
vcharlesAuthor Commented:
Hi,

I'm sorry for the late reply. How would you do it from the DataGridview after loading the table in the Grid?

Thanks,

Victor
0
Jacques Bourgeois (James Burger)PresidentCommented:
I do not work with TrueDBGrid. It might have a feature to do that. I would first look for something like "group" or "aggregation" in the documentation.

In the standard DataGridView, I would use the following algorithm.

Add an extra invisible Boolean column in your table, with a value of False by default.

Either retrieve the data from the database or use a sort or DataView in the application to sort the table on the field(s) on which you want to group.

Loop through the rows. For each one, compare the value in the specified field(s) to the value of those fields in the previous and next row. If they are the same, set you extra column to True to mark it as identical to another one.

Set your filter so that it returns only the rows that have True in the extra column.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vcharlesAuthor Commented:
Can you please send me the code using the DatagridView.
0
Jacques Bourgeois (James Burger)PresidentCommented:
I do not have the exact details and I do not have time to setup a test environment and test and debug my code, but it should look something like the following:

                Dim FilteredDT As DataTable  = New DataView(dta, "Receiver like " & "'" & C1Receiver.Columns(0).Value & "'" & "",  "SN,NSC", DataViewRowState.CurrentRows).ToTable

		With FilteredDT

			.Columns.Add("Flag", GetType(Boolean))
			.Columns("Flag").DefaultValue = False

			For x As Integer = 0 To .Rows.Count - 1
				If x < .Rows.Count - 1 Then	'Compare with next row
					If CInt(.Rows(x).Item("SN")) = CInt(.Rows(x + 1).Item("SN")) AndAlso CStr(.Rows(x).Item("SN")) = CStr(.Rows(x + 1).Item("SN")) Then
						.Rows(x).Item("Flag") = True
						.Rows(x).Item("Flag") = True
					End If
				End If
			Next

		End With

		FilteredDT = New DataView(FilteredDT, "Flag=True", "", DataViewRowState.Added).ToTable

                C1TrueDBGrid45.DataSource = FilteredDT

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
0
Jacques Bourgeois (James Burger)PresidentCommented:
A small mistake:

.Rows(x).Item("Flag") = True
.Rows(x+1).Item("Flag") = True

I forgot the +1 on the second line
0
vcharlesAuthor Commented:
Thank You.
0
vcharlesAuthor Commented:
Hi Jacques,

How do you modify the code below to work with a DataGridView?

 Dim FilteredDT As DataTable  = New DataView(dta, "Receiver like " & "'" & C1Receiver.Columns(0).Value & "'" & "",  "SN,NSC", DataViewRowState.CurrentRows).ToTable

.Value is a not a property of the DataGridView?

Thanks,

Victor
0
Jacques Bourgeois (James Burger)PresidentCommented:
In a DataGridView, Columns does not represent the data, it represents the caracteristics of the columns: header, width, font to use in the cells and the likes.

To get a specific piece of data, you should go C1Receiver.Item(0,x).Value, where 0 is the column and x the row that contains the value that you want to retrieve.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.