How to make a search textbox by type in DataGridView using VB.NET
I have connected my DataGridView to the database, but I cannot implement the search functionality.
The program flow will be when I click on one column of the DataGridView and I type in the search box, I can only get the results from the same column besides other columns.
It also has to search for the letter by letter, so basically it's the TextChanged event.
source to share
This is how I will do it
First, you have two variables to store the original data from the database, as well as a string variable to store the selected dgv column header header (which will be used for the subsequent filter).
Private oriDataTable As New DataTable
Private columnToFilter As String = String.Empty
My test for some dummy data
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'dummy datatable
oriDataTable.Columns.Add(New DataColumn("ID"))
oriDataTable.Columns.Add(New DataColumn("FirstName"))
oriDataTable.Columns.Add(New DataColumn("LastName"))
For i = 0 To 5
Dim dr As DataRow = oriDataTable.NewRow()
dr.Item("ID") = i
dr.Item("FirstName") = "fn type1 " & i
dr.Item("LastName") = "ln type1 " & i
oriDataTable.Rows.Add(dr)
Next
For i = 6 To 10
Dim dr As DataRow = oriDataTable.NewRow()
dr.Item("ID") = i
dr.Item("FirstName") = "fn type2" & i
dr.Item("LastName") = "ln type2" & i
oriDataTable.Rows.Add(dr)
Next
'Since you already connected to database
'i assume that you could fill a datatable and bind to dgv
dgvToFilter.DataSource = oriDataTable
columnToFilter = "ID" 'Assign any default column name
End Sub
Then add a ColumnHeaderMouseClick event handler to your dgv, update columnToFilter
every time the user clicks on it.
Private Sub dgvToFilter_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvToFilter.ColumnHeaderMouseClick
Dim clickedColumn As DataGridViewColumn = dgvToFilter.Columns(e.ColumnIndex)
'Note:HeaderText must match with your datatable column name
columnToFilter = clickedColumn.HeaderText
lblHeaderSelected.Text = columnToFilter
End Sub
Finally, the TextChaged event. Use the DataTable.Select method to filter the data and update the result, if any, for the dgv.
Private Sub txtFilterText_TextChanged(sender As Object, e As EventArgs) Handles txtFilterText.TextChanged
If txtFilterText.Text.Length <= 0 Then dgvToFilter.DataSource = oriDataTable
Dim filterString = String.Format("{0} LIKE '{1}%'", columnToFilter, txtFilterText.Text)
Dim dataRows As DataRow() = oriDataTable.Select(filterString)
'Choose what you wan to do if no row is found. I bind back the oriDataTable.
dgvToFilter.DataSource = If(dataRows.Count > 0, dataRows.CopyToDataTable(), oriDataTable)
End Sub
source to share
You can try this.
Private Sub txtUname_TextChanged(sender As Object, e As EventArgs) Handles txtUname.TextChanged dtaAdap = New SqlDataAdapter("Select * from tbl_user where Fname like '%" & txtUname.Text & "%'" & vbCrLf & " OR Lname like '%" & txtUname.Text & "%'", con) dt = New DataTable dtaAdap.Fill(dt) DataGridView1.DataSource = dt End Sub
The query in SQLAdapter looks like this:
Select * from <tbl_name> where <firstparametercolumnname> like '%"& <your searchtexboxname.text here> &"%'
OR <secondparametercolumnname> like '%"& <your searchtexboxname.text here> &"%'
etc. depending on the number of fields you want to look at. Note: "con" is my SQLConnection.
This whole piece of code will populate your DatagridView with the query result every time the user enters something into your searchtextbox.
source to share