Dataview rowfilter not working

Posted on 2008-10-15
Last Modified: 2013-12-26
I have a dataset which I pass to a Dataview for filtering. I then pass that into a function that returns a datatable. The problem is my filter does not return anything! Dv.count = 0.

Can anyone tell from the code below why this is not working??
Dim dv As New DataView(oDS.Tables(0))

'Passed in full dataset for filtering

            Dim trimmedcity As String() 'string array

            trimmedcity = cityname.Split(",")

            cityname = trimmedcity(0)

            With dv

                .Table = oDS.Tables(0)

                .AllowDelete = True

                .AllowEdit = True

                .AllowNew = True

                .RowFilter = "City='%" & cityname & "%'".Trim

                .RowStateFilter = DataViewRowState.ModifiedCurrent


            End With

            'pass filtered dataset 

            'Create a datatavle

            Dim dt As New DataTable

            dt = CreateTable(dv.Table.DefaultView)


             End If

        Return DataSetToSiteSet(oDS)

'Create table code

Public Shared Function CreateTable(ByVal obDataView As DataView) As DataTable

        If obDataView Is Nothing Then

            Throw New ArgumentNullException("DataView", "Invalid DataView object specified")

        End If

        Dim obNewDt As DataTable = obDataView.Table.Clone()

        Dim idx As Integer = 0

        Dim strColNames As String() = New String(obNewDt.Columns.Count - 1) {}

        For Each col As DataColumn In obNewDt.Columns

            '    strColNames(System.Math.Max(System.Threading.Interlocked.Increment(idx), idx - 1)) = col.ColumnName

            strColNames(idx) = col.ColumnName


        Dim viewEnumerator As IEnumerator = obDataView.GetEnumerator()

        While viewEnumerator.MoveNext()

            Dim drv As DataRowView = DirectCast(viewEnumerator.Current, DataRowView)

            Dim dr As DataRow = obNewDt.NewRow()


                For Each strName As String In strColNames

                    dr(strName) = drv(strName)


            Catch ex As Exception


            End Try


        End While

        Return obNewDt

    End Function

Open in new window

Question by:peterdidow
  • 2
  • 2

Expert Comment

Comment Utility
Try .RowFilter = "City Like'%" & cityname & "%'"
LVL 14

Expert Comment

by:Shiju Sasidharan
Comment Utility
or try   this
    'Usage   dv = FilterDataTable(oDS.Tables(0), "City", "%" & cityname.Trim & "%", " LIKE ", false)

    Public Function FilterDataTable(ByVal pdtData As DataTable, ByVal pstrConditionField As String, ByVal pstrConditionFieldValue As String, ByVal pstrCondition As String, ByVal pblnIsNumericField As Boolean) As DataView

        Dim dvResult As New DataView(pdtData)

        Dim strCondition As String

        If Not pblnIsNumericField Then

            strCondition = pstrConditionField & pstrCondition & "'" & pstrConditionFieldValue.Replace("'", "''") & "'"


            strCondition = pstrConditionField & pstrCondition & pstrConditionFieldValue

        End If

        dvResult.RowFilter = strCondition

        FilterDataTable = dvResult

    End Function

Open in new window


Author Comment

Comment Utility
Actually this was the issue for the rowcount being zero for the filtered dv:

.RowStateFilter = DataViewRowState.ModifiedCurrent

which I changed to
   .RowStateFilter = DataViewRowState.CurrentRows
which returns the filtered data as desired.

I like the function you wrote, but I need a DataTable returned, not a Dataview

LVL 14

Accepted Solution

Shiju Sasidharan earned 500 total points
Comment Utility
inside the function do this

FilterDataTable = dvResult.ToTable

and the change its return type to DataTable

Author Closing Comment

Comment Utility
Yep, thats what I ended up doing. Thanks for your help!

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now