Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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.
Avatar of Victor  Charles

ASKER

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
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.
Can you please send me the code using the DatagridView.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You.
A small mistake:

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

I forgot the +1 on the second line
Thank You.
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
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.