Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dataview rowfilter not working

Posted on 2008-10-15
5
Medium Priority
?
2,377 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
Suggested Courses

618 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