DataView doesn´t filter

M

Martin

I am, in two different codes, using DataView.RowFilter. In one it does work
but in the other it does not. I dont recive any Error Messages, the code
runs thru and delivers a result, unfiltered. Number one looks like this:

Dim dvHours As New DataView

dvHours.Table = DS.Tables(1)

dvHours.RowFilter = "StoreId=" & .RecordId

dvHours.Sort = "ViewOrder"


And number two, the one that doesn´t work, look like this:

Dim DV As New DataView

'With DV

DV.Table = Ds.Tables(0) '.Copy

DV.RowFilter = "intDate=" & RegDate '.ToString

DV.Sort = "StoreId" As

'End With

As you can see, I do have tried different solutions on the second one. What
ever I tried, the result is still the same, the DataView keep all the
records as it had before the RowFilter is set. Thanks for any hint.
 
J

Jon Skeet [C# MVP]

Martin said:
I am, in two different codes, using DataView.RowFilter. In one it does work
but in the other it does not. I dont recive any Error Messages, the code
runs thru and delivers a result, unfiltered.

You've shown how you're setting the filter, but not how you're
displaying the rows within the view. Could you tell us what you're
doing there?
 
M

Martin

Thanks for your interest
I'm not sure about your question though. What do you mean with displaying
the rows? In what order the columns comes or how I have filled the DataSet /
Table from which the DataView is created? Could you enlight me a bit?

Martin
 
J

Jon Skeet [C# MVP]

Martin said:
Thanks for your interest
I'm not sure about your question though. What do you mean with displaying
the rows? In what order the columns comes or how I have filled the DataSet /
Table from which the DataView is created? Could you enlight me a bit?

Well, you said your code "runs thru and delievers a result" but you
haven't said what exactly it's doing. How are you getting the data out
of the view, when you've set the filter?
 
M

Martin

Ok

At the end I want to put the filtered DataView back in a DataSet. Thats just
to fit withn what is allready done and not because it is a good idea. From
the DatSet I populate a list. for the moment that is not interessting
though, since I spot the result, in test purpose, with the following routine
call and routine. I then see the result in a file.

-- Routine Call:

Dim oLog As New Utilities.Log

oLog.dumpDataViewToFile("DailyData", DV)

-- Routine:

Public Sub dumpDataViewToFile(ByVal Filename As String, ByVal dsToDump As
DataView)

Dim iCnt As Integer, I As Integer

Dim sStr As String

Dim Path As String =
Configuration.ConfigurationSettings.AppSettings.Get("logfilepath")

Dim T As Data.DataTable

Dim R As Data.DataRow

Dim C As Data.DataColumn

Dim SW As StreamWriter

Filename = Filename & "DataView.txt"

SW = File.CreateText(Path & Filename)

T = dsToDump.Table

SW.WriteLine(T.TableName)

sStr = ""

For Each C In T.Columns

If sStr <> "" Then sStr = sStr & Chr(9)

sStr = sStr & C.ColumnName

Next

SW.WriteLine(sStr)

iCnt = T.Columns.Count

For Each R In T.Rows

sStr = ""

For Each C In T.Columns

If sStr <> "" Then sStr = sStr & Chr(59) & Chr(9)

If IsDBNull(R.Item(C.ColumnName)) Then sStr = sStr & "" Else sStr = sStr &
CType(R.Item(C.ColumnName), String)

If C.DataType Is GetType(Integer) Then sStr = sStr & Chr(9)

Next

SW.WriteLine(sStr)

Next

SW.Close()

End Sub
 
P

Patrice

You are actually browsing the Rows collection of the parent DataTable object
that always contains the unfiltered data. You should use dsToDump.Item to
browse filtered records.

Patrice


Martin said:
Ok

At the end I want to put the filtered DataView back in a DataSet. Thats just
to fit withn what is allready done and not because it is a good idea. From
the DatSet I populate a list. for the moment that is not interessting
though, since I spot the result, in test purpose, with the following routine
call and routine. I then see the result in a file.

-- Routine Call:

Dim oLog As New Utilities.Log

oLog.dumpDataViewToFile("DailyData", DV)

-- Routine:

Public Sub dumpDataViewToFile(ByVal Filename As String, ByVal dsToDump As
DataView)

Dim iCnt As Integer, I As Integer

Dim sStr As String

Dim Path As String =
Configuration.ConfigurationSettings.AppSettings.Get("logfilepath")

Dim T As Data.DataTable

Dim R As Data.DataRow

Dim C As Data.DataColumn

Dim SW As StreamWriter

Filename = Filename & "DataView.txt"

SW = File.CreateText(Path & Filename)

T = dsToDump.Table

SW.WriteLine(T.TableName)

sStr = ""

For Each C In T.Columns

If sStr <> "" Then sStr = sStr & Chr(9)

sStr = sStr & C.ColumnName

Next

SW.WriteLine(sStr)

iCnt = T.Columns.Count

For Each R In T.Rows

sStr = ""

For Each C In T.Columns

If sStr <> "" Then sStr = sStr & Chr(59) & Chr(9)

If IsDBNull(R.Item(C.ColumnName)) Then sStr = sStr & "" Else sStr = sStr &
CType(R.Item(C.ColumnName), String)

If C.DataType Is GetType(Integer) Then sStr = sStr & Chr(9)

Next

SW.WriteLine(sStr)

Next

SW.Close()

End Sub
 
J

Jon Skeet [C# MVP]

Martin said:
At the end I want to put the filtered DataView back in a DataSet.

You can't. A DataSet doesn't contain DataViews, it contains DataTables.

The problem with the code you produced (which was as I expected it
would be, to be honest) is that you're showing all the rows in the
*table*, not all the rows in the *view*. A view is a filtered look at a
table - it doesn't change the data in the table at all.

If you change

For Each R in T.Rows

to

For Each R in dsToDump

you'll probably find it's fine. (I haven't tried it though.)
 
M

Martin

Thanks both of you.

I got it to work using the Item as Patrice said. A DataView has no rows as I
saw it. I could not use the For Each loop but had to switch to a For I=0 To
DV.Count-1 loop.

Martin

Martin said:
Ok

At the end I want to put the filtered DataView back in a DataSet. Thats just
to fit withn what is allready done and not because it is a good idea. From
the DatSet I populate a list. for the moment that is not interessting
though, since I spot the result, in test purpose, with the following routine
call and routine. I then see the result in a file.

-- Routine Call:

Dim oLog As New Utilities.Log

oLog.dumpDataViewToFile("DailyData", DV)

-- Routine:

Public Sub dumpDataViewToFile(ByVal Filename As String, ByVal dsToDump As
DataView)

Dim iCnt As Integer, I As Integer

Dim sStr As String

Dim Path As String =
Configuration.ConfigurationSettings.AppSettings.Get("logfilepath")

Dim T As Data.DataTable

Dim R As Data.DataRow

Dim C As Data.DataColumn

Dim SW As StreamWriter

Filename = Filename & "DataView.txt"

SW = File.CreateText(Path & Filename)

T = dsToDump.Table

SW.WriteLine(T.TableName)

sStr = ""

For Each C In T.Columns

If sStr <> "" Then sStr = sStr & Chr(9)

sStr = sStr & C.ColumnName

Next

SW.WriteLine(sStr)

iCnt = T.Columns.Count

For Each R In T.Rows

sStr = ""

For Each C In T.Columns

If sStr <> "" Then sStr = sStr & Chr(59) & Chr(9)

If IsDBNull(R.Item(C.ColumnName)) Then sStr = sStr & "" Else sStr = sStr &
CType(R.Item(C.ColumnName), String)

If C.DataType Is GetType(Integer) Then sStr = sStr & Chr(9)

Next

SW.WriteLine(sStr)

Next

SW.Close()

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top