Solved

Dataview rowfilter not working

Posted on 2008-10-15
5
2,351 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Formula problem with Excel attachment 6 38
VB.NET 2008 - SQL Timeout 9 34
Very Large data in MYSQL 7 73
Vb.net threads keep increasing 2 35
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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