How to make a filter using LIKE on fields of type int in a DataView?

1

I'm using the code below to make a filter in my DataGridView , but when I do the filter using like in a field of type int , I get the following error:

  

Can not perform 'Like' operation on System.Int32 and System.String.

Follow the code:

Private Sub txtProcura_TextChanged(sender As Object, e As EventArgs) Handles txtProcura.TextChanged
    If dt.Rows.Count = 0 Then Exit Sub
    Try
        Select Case cboxFiltro.SelectedItem
            Case Is = "Pedidos"
                dv = New DataView(dt, "Pedido like '%" & txtProcura.Text & "%'", "Pedido asc", DataViewRowState.OriginalRows)
            Case Is = "Chave"
                dv = New DataView(dt, "chave like '%" & txtProcura.Text & "%'", "Pedido asc", DataViewRowState.OriginalRows)
        End Select

        dgvPedidosSAT.DataSource = dv
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Would anyone have a solution?

    
asked by anonymous 24.12.2014 / 15:44

1 answer

1

In this case you need to convert your column to String , doing so:

dv = New DataView(dt, "CONVERT(Pedido, 'System.String') like '%" & txtProcura.Text & "%'",
                    "Pedido asc", DataViewRowState.OriginalRows)

Another way to do what you want is through DataTable itself, like this:

dt.DefaultView.RowFilter = "CONVERT(Pedido, 'System.String') like '%" & txtProcura.Text & "%'"
dt.DefaultView.Sort = "Pedido asc"

An important detail about like is that * or % characters can not be used in the middle of their expression, for example:

'%123' - válido
'123%' - válido
'%12%' - válido
'1%34' - inválido

You can take a look at on this link to learn more about the accepted expressions.

    
24.12.2014 / 16:32