Solved

Dataview rowfilter not working

Posted on 2008-10-15
5
2,356 Views
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)
 
            oDS.Tables.Clear()
             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
        Next
 
        Dim viewEnumerator As IEnumerator = obDataView.GetEnumerator()
        While viewEnumerator.MoveNext()
            Dim drv As DataRowView = DirectCast(viewEnumerator.Current, DataRowView)
            Dim dr As DataRow = obNewDt.NewRow()
            Try
                For Each strName As String In strColNames
                    dr(strName) = drv(strName)
                Next
            Catch ex As Exception
                Trace.WriteLine(ex.Message)
            End Try
            obNewDt.Rows.Add(dr)
        End While
 
        Return obNewDt
    End Function

Open in new window

0
Comment
Question by:peterdidow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22726729
Try .RowFilter = "City Like'%" & cityname & "%'"
0
 
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("'", "''") & "'"
        Else
            strCondition = pstrConditionField & pstrCondition & pstrConditionFieldValue
        End If
        dvResult.RowFilter = strCondition
        FilterDataTable = dvResult
    End Function

Open in new window

0
 

Author Comment

by:peterdidow
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

0
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 500 total points
ID: 22732892
inside the function do this

FilterDataTable = dvResult.ToTable

and the change its return type to DataTable
0
 

Author Closing Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

726 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