Link to home
Start Free TrialLog in
Avatar of peterdidow
peterdidowFlag for Canada

asked on

Dataview rowfilter not working

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

Avatar of tiagosalgado
tiagosalgado
Flag of Portugal image

Try .RowFilter = "City Like'%" & cityname & "%'"
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

Avatar of peterdidow

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Shiju S
Shiju S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, thats what I ended up doing. Thanks for your help!