[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2388
  • Last Modified:

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

0
peterdidow
Asked:
peterdidow
  • 2
  • 2
1 Solution
 
tiagosalgadoCommented:
Try .RowFilter = "City Like'%" & cityname & "%'"
0
 
Shiju SasidharanCommented:
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
 
peterdidowAuthor Commented:
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
 
Shiju SasidharanCommented:
inside the function do this

FilterDataTable = dvResult.ToTable

and the change its return type to DataTable
0
 
peterdidowAuthor Commented:
Yep, thats what I ended up doing. Thanks for your help!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now