Victor Charles
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).Valu e & "'" & "", Nothing, DataViewRowState.CurrentRo ws)
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
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).Valu
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
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.
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'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.
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.
ASKER
Can you please send me the code using the DatagridView.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You.
A small mistake:
.Rows(x).Item("Flag") = True
.Rows(x+1).Item("Flag") = True
I forgot the +1 on the second line
.Rows(x).Item("Flag") = True
.Rows(x+1).Item("Flag") = True
I forgot the +1 on the second line
ASKER
Thank You.
ASKER
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).Valu e & "'" & "", "SN,NSC", DataViewRowState.CurrentRo ws).ToTabl e
.Value is a not a property of the DataGridView?
Thanks,
Victor
How do you modify the code below to work with a DataGridView?
Dim FilteredDT As DataTable = New DataView(dta, "Receiver like " & "'" & C1Receiver.Columns(0).Valu
.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.
To get a specific piece of data, you should go C1Receiver.Item(0,x).Value