Author Topic: DataView contains no records after filter  (Read 1550 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
DataView contains no records after filter
« on: August 12, 2010, 05:04:33 PM »
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.


Code: [Select]
Sub RecordCnt ()
  Dim dt As New DataTable
  dt = Me.MyDataSet1.Names
  Debug.Print("Row count")
  Debug.Print(dt.Rows.Count) '<- this always 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 then 0 from then on
End Sub

I'd appreciate any insight as to why this doesn't work
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: DataView contains no records after filter
« Reply #1 on: August 12, 2010, 05:50:23 PM »
I think what's happening is that dt is being modified by the dt.DefaultView.ToTable.... which changes Names.
So DefaultView is different the second and subsequent times through.

Not sure of the VB syntax but maybe try dt = Me.MyDataSet1.Names.Copy()
That way you won't effect the table stored in Names.
dt will be fresh each time RecordCnt is called.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions