Filter words contained in Bindingsource

Asked

Viewed 114 times

1

I need to filter in my DataGridView which has as the datasource one BindingSource (filtro_descricao). I need to search the lines for the text typed in textbox1 regardless of the order they are in the columns, for example:

Typing "Baby" I find two lines.

inserir a descrição da imagem aqui

But if I type "White" which is a color, in front of "Baby" without the "look", the filter does not find anything. Look:

inserir a descrição da imagem aqui

This is the command I use to filter:

 Private Sub BuscaProduto()

    frm_gerar_pedido.filtro_descricao.Filter =
        "[cat_produto] Like'%" + TextBox1.Text + "%'" _
        & "or [descricao_produto] Like'%" + TextBox1.Text + "%'" _
        & "or [formato] Like'%" + TextBox1.Text + "%'" _
        & "or [cor] Like'%" + TextBox1.Text + "%'" _
        & "or [tamanho] Like'%" + TextBox1.Text + "%'" _
        & "or [manga] Like'%" + TextBox1.Text + "%'"
End Sub

What can it be?

  • 2

    If you filter by "White Baby," you’re not filtering out two separate words "Baby" and "White," you’re looking in every column for something that contains "White Baby" in the cell.

  • 1

    What is the purpose of the research "white baby"? Appear in the same two rows because there are columns that have the word "baby" and "white", or just the first because it’s only which has two columns with "baby" and "white"?

1 answer

3

You have to make a loop to resort to the columns make the filter and use a If/Else to determine the number of rows returned in the DataGrid if it is equal to zero pass to the next one if it is larger then displays in the DataGrid and move to the next column.

Think about the algorithm and write your code. You can use the DataGridViewCell or DataGridViewColumn to use the columns in foreach.

And to get the number of lines is DataGridView.Rows.Count I’d use a DataTable to store the searched lines and then pass the DataTable to the DataGridView.DataSource.

But if I were you I’d put one ComboBox with the name of the columns for the user to choose in which column to make the filter that would be much easier and simple.

Browser other questions tagged

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