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

ID: 22726729
Try .RowFilter = "City Like'%" & cityname & "%'"
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 22732272
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

ID: 22732769
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
ID: 22732892
inside the function do this

FilterDataTable = dvResult.ToTable

and the change its return type to DataTable

Author Closing Comment

ID: 31506537
Yep, thats what I ended up doing. Thanks for your help!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to measure sizes and angles in scanned images ? 3 53
Connect to a database from Excel using JDBC instead of ODBC 3 47
vbModal 12 35
Vb. Net application freezes 9 30
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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