Question search in Datagridview using TextBox - without using Database Connectivity

karnee

New member
Joined
Mar 27, 2014
Messages
3
Programming Experience
3-5
I need your assistance to filter the values in Datagrid by using text box.
Whenever the I type the value in Text-box, it filters the values in data grid without using any database connectivity. I got the below coding but it is not working and it is not filtering anything.

Please provide me the code to do the search the content in Data grid.


Public Sub txtsearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtsearch.TextChanged

Dim Flag As Boolean
If Flag = True Then
dgDisplay.ClearSelection()
For Each row As DataGridViewRow In dgDisplay.Rows
For Each cell As DataGridViewCell In row.Cells
If Not IsNothing(cell.Value) Then
If cell.Value.ToString.StartsWith(txtsearch.Text, StringComparison.InvariantCultureIgnoreCase) Then
cell.Selected = True
dgDisplay.CurrentCell = dgDisplay.SelectedCells(0)
'Exit For
End If
End If
Next
Next
Else
If txtsearch.Text = "" Then
dgDisplay.Rows(0).Selected = True
End If
End If
End Sub
 
Dim Flag As Boolean 'By default, Flag now = false
Then immediately you are checking to see if Flag = True and it doesn't so that block won't run.

Learning to step-debug through the program and see how the code is operating and what variables contain would have made this obvious.
 
Yes you're right

Dim Flag As Boolean 'By default, Flag now = false
Then immediately you are checking to see if Flag = True and it doesn't so that block won't run.

Learning to step-debug through the program and see how the code is operating and what variables contain would have made this obvious.


I changed the FLAG value to TRUE ans start the search but it is not working. can you please check and provide me the code for that.
 
You'll have to define "but it is not working" because the code above would start searching as you type in the textbox, finding the LAST entry that matches. Perhaps you want this to work another way. If you're talking about limiting rows then you're going to want to look at getting this data into at least a datasource and limiting the rows via a default view or similar, while the datagrid pointing to that datasource. The very least method would be to hide all rows and then unhide rows that meet your criteria.
 
New to this...

You'll have to define "but it is not working" because the code above would start searching as you type in the textbox, finding the LAST entry that matches. Perhaps you want this to work another way. If you're talking about limiting rows then you're going to want to look at getting this data into at least a datasource and limiting the rows via a default view or similar, while the datagrid pointing to that datasource. The very least method would be to hide all rows and then unhide rows that meet your criteria.


Actually i am new to this topic that's the reason i am stuck in this. It would be helpful if i get the code from you to do this task.
 
Which task? I mentioned a few methods. The code you have is very close to being able to perform the last option I listed. As it stands right now, I'm still not sure I understand what you want to achieve. You described one thing, but the code you showed did something else which tells me that you haven't tried to understand the code and what it actually does.
 
The simplest way to implement filtering is to use a DataTable. While DataTables are most commonly used as an in-memory representation of data from a database, there is no requirement that they be so. A DataTable is just a data structure like any other and you can create one, build it's schema and populate it yourself without a database in sight.

Assuming that you have a populated DataTable, you can bind it to a BindingSource and then bind that to the grid:
myBindingSource.DataSource = myDataTable
myDataGridView.DataSource = myBindingSource
You then simply set the Filter property of the BindingSource and the data will be filtered automatically, e.g.
myBindingSource.Filter = String.Format("SomeColumn LIKE '%{0}%'", myTextBox.Text)
 

Latest posts

Back
Top