BP Forums
Filter each cell in dgv - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Programming Discussion (https://bpforums.info/forumdisplay.php?fid=34)
+--- Forum: VB.NET (https://bpforums.info/forumdisplay.php?fid=43)
+--- Thread: Filter each cell in dgv (/showthread.php?tid=899)



Filter each cell in dgv - t3cho - 04-24-2015

I have the datagridview on my form and i want to filter it for current text in textbox1.

Example i have fields
Name,Lastname,Location,Town,Address.

And if i enter in textbox1 value "John" i need to check each of those columns does any contain a value John and show on dgv.

Anel


RE: Filter each cell in dgv - brandonio21 - 04-24-2015

How is the data in the DataGridView stored? Is it stored using a DataSource or simply being directly added into the DataGridView? If you're using a datasource, filtering becomes fairly easy, as you can do something like this:


Code:
CType(dgv.DataSource, DataTable).DefaultView.RowFilter = TextBox1.Text
dgv.Refresh()

If you're just inputting things in the Data Grid View direclty, I think you will need to go through every row, check to see if the column is equal to the textbox value, and eliminate those that are not equivalent.


RE: Filter each cell in dgv - t3cho - 04-25-2015

I'm inputting it directly on DGV by using SQL Query. After i get the data by using sql query data was in dataset. After i use dgv source to get the data from dataset to dgv.

But the problem would be when i enter two values in textbox1.text. In that case i need result which contains in two columns those values.

ex: Bran Prod

It should give me result where it contains Name & location or any other two columns.


RE: Filter each cell in dgv - brandonio21 - 04-26-2015

Ah, okay.. You're probably looking for something like this:

Please note that this is Pseudocode, because I do not know the official syntax of VB.NET DGV offhand:

Code:
dvg.SelectionMode = FullRowSelect
For Each Row as DataGridViewRow in dvg.Rows
    For Each Cell as DataGridViewCell in Row.Cells
        If (Cell.Value.ToString().Equals(TextBox1.Text))
            Row.Selected = True
            Break
        End If
     Next
Next


This should check all of the cells for the values being searched and select only the rows containing that information.


RE: Filter each cell in dgv - brco900033 - 05-08-2015

Hi,

I wanted to add a small remark:
Break is VB code, the equivalent in VB.NET is Exit

In this case it would be Exit For, but you can use it in a lot of situations:
  • Exit For
  • Exit Do
  • Exit While
  • Exit Function
  • Exit Sub