How to make a filter using LIKE in int type fields in a Dataview?

Asked

Viewed 1,063 times

1

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

Cannot perform 'Like' operation on System.Int32 and System.String.

Follows 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

Someone would have a solution?

  • dt is a DataTable?

  • That’s right, Matthew!

  • Not necessarily, I can vary, but I’m using this because I did a query and it worked perfectly, I just didn’t understand why it didn’t work with Dataview. What is your suggestion ?

1 answer

1


In that 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 yourself DataTable, thus:

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

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

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

You can take a look at that link to learn more about accepted expressions.

  • It worked Matthew! Thank you very much indeed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.