Dataview contains no records after filter

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
I have a datatable that I fill when my application loads. This table is used to generate dataviews on the fly so I can maintain a listbox of unique values in the table.

I have the following code that works well the first time it is used, then shows no records after the second time. I am scratching my head over this one and for the life of me I can't see the problem. Maybe my understanding of tables and views is not up to par, but it looks as though it should work.


VB.NET:
Sub RecordCnt () 
  Dim dt As New DataTable
  dt = Me.MyDataSet1.Names
  Debug.Print("Row count")
  Debug.Print(dt.Rows.Count) '<- this returns 1415 rows
  Dim cf(0) As String
  cf(0) = "Route"
  Dim dv As New DataView
  dv = dt.DefaultView.ToTable(True, cf).DefaultView
  Debug.Print("Record count")
  Debug.Print(dv.Count) '<- this returns 24 the first time I call this code 0 all others
End Sub
 
Uhm... I think a datarow cannot belong to more than one table and the help docs don't say that the rows are cloned,m merely that "the resulting table contains the filtered rows"

Ergo: you have 1000 rows, 100 of which are "bob"
You filter on bob and ToTable them
You now have a table wil 900 rows and a table with 100 "bob" rows
Any further attempts to get "bob" rows out of the 900 will return 0 rows, because there are none

I suggest you re-work what youre doing. If you want a dataview, just make a new one pointed at the main table:

VB.NET:
Private m_allRows as DataTable 'fill it however, and it lives at class level

' later in code
Dim dvBob as New DataView(m_allRows)
dvBob.Filter = "[name] = 'bob'"

Dim dvJoe as New DataView(m_allRows)
dvBob.Filter = "[name] = 'joe'"
See the DataView documentation for more info
 
ps; views (in databses or here in vb.net) NEVER "contain" data.. They are simply showing a subset of data they find somewhere else. The data never lives in the view, just like a photograph of a building is not a building you can walk into
 
Thanks for the reply, but alas, the error hasn't anything to do with there not being any data to match the filter.

1) I have 1000 rows with "bob" in 100 so I filter for "bob" and end up with 100 rows ... this works as expected
2) the next time this function is called I reload from the datatable and find 1000 rows, then filter for "bob" on those 1000 rows and I get 0 rows, when in fact there are 100 rows with "bob", as evidenced by the first call.

The question is, if all calls to retrieve records returns 1000 rows, and if there are indeed 100 "bob" rows, why does it return 100 the first time and each subsequent time I work on those same 1000 rows, (remember I create a new view from the table each time) I get 0 rows.
 
Post a complete solution replicating the issue and I'll tell you ;)

Alternatively just go the prescribed way of downloading the data once and attaching the relevant filters to it. Actually, you can fill and refill the relevanttable as much as you want and leave all the filters attached to it and they will just update every time you change the data. i.e. don't try to turn views into tables etc - use them for their proper purpose. If you want a list of rows matching some criteria, use the datatable.Select() method
 
Back
Top