VB.NET - Dataview Rowfilter
Asked By Sheila Bailey on 23-Jun-04 01:12 PM
How do I build a dataview.rowfilter that will return a row for each data field1 and the max in data field2? I tried the following, but it did not work(always returned one row - din 3 uniqueidentifier):
dv.rowfilter = "field1 like '%' and max(field2)"
ex:
datatable:
field1 field2 field3
brk 1 uniqueidentifier
lun 1 uniqueidentifier
din 1 uniqueidentifier
brk 2 uniqueidentifier
din 2 uniqueidentifier
din 3 uniqueidentifier
sup 1 uniqueidentifier
dataview:
field1 field2 field3
brk 2 uniqueidentifier
lun 1 uniqueidentifier
din 3 uniqueidentifier
sup 1 uni1ueidentifier
Dataview Rowfilter
Asked By Sheila Bailey on 23-Jun-04 01:21 PM
Correction, I set the dv.rowfilter property to "field1 like '%' and field2 = max(field2)"
I don't think you can do this in RowFilter
Asked By Robbe Morris on 23-Jun-04 03:38 PM
You'd have better luck with the original SQL Statement to populate the filter. Your requirement needs more than can be accomplished in the .RowFilter method which is essentially the "WHERE" clause of a query.
If you aren't allowed to do that, then you'll have to iterate through the rows of the dataView and delete records that have a "field2" less than the max "field2" found so far.
It least, this way you can pass on the DataView to whatever UI or reporting control is expecting it as the data source.
I'd get the Max value first,
Asked By Peter Bromberg on 23-Jun-04 03:42 PM
since it will always be the same.
You could take a dataview and sort descending and get the first row for the myMax variable to store it in,
then you would use field2 =myMax in your rowfilter.
I think she wants
Asked By Robbe Morris on 23-Jun-04 04:01 PM
a separate row for each Field1 and max(Field2) combination.
field1 field2
a 2
a 1
b 3
b 2
should return only these two rows
a 2
b 3
I don't see how you can do that with just the .RowFilter alone and not iterating
through the rows.
you r right!
Asked By Peter Bromberg on 23-Jun-04 06:32 PM
you can filter but you can't return data that's not in the row that you r filtering out with your filter. (how much wood could a woodchuck chuck if a woodchuck could chuck wood?)
Perhaps Sheila can clue us in.
Dataview Rowfilter
Asked By Sheila Bailey on 24-Jun-04 09:11 AM
I have abandoned the idea if using rowfilter. What I am doing now is cloning my datatable. Creating a dataview of the orginial datatable and sorting it in the order that I need it (seqNo, DayNumber). Then I loop thru the dv and importing the last dataview row into the cloned datatable.
Protected Overridable Function GetDeleteMealCandidates() As DataSet
Dim ds As New DataSet()
Dim dt As DataTable = Me.m_dsFieldMenu.Tables("dtMenuMeals").Clone
Dim dv As New DataView(Me.m_dsFieldMenu.Tables("dtMenuMeals"))
Dim dr As DataRow
Dim drHold As DataRow
Dim indx As Integer = 0
Dim seqNo As Integer = 0
dv.sort = "SeqNo, DayNumber"
Try
For indx = 0 To dv.Count - 1
dr = dv(indx).Row
If Not dr("SeqNo").Equals(seqNo) Then
If seqNo > 0 Then
dt.ImportRow(drHold)
End If
seqNo = DirectCast(dr("SeqNo"), Integer)
End If
drHold = dr
Next
dt.ImportRow(drHold)
ds.Tables.Add(dt)
Return ds
Catch ex As Exception
Throw ex
End Try
End Function