I have a program that looks like this.
If I click the button search on the top form, a new form will show like this.
If I put a data inside the textbox and press find the program will find the nearest data and put color on it. like this.
and this is the code for that (Given by the one of the best programmer that I know)
If TextBox1.Text = "" Then
MsgBox("Nothing to search.")
Dim drow() As DataRow
drow = dt.Select("ItemCode LIKE '%" & TextBox1.Text & "%' OR Description LIKE '%" & TextBox1.Text & "%'")
If drow.Count > 0 Then
'THIS WILL LOOP THE GRID BASED ON THE RECORDS FOUND BY TXTSEARCH.TEXT
For Each row As DataRow In drow
Dim ItemCodeStr As String = row.Item("ItemCode").ToString
For Each dgrow As DataGridViewRow In Variance.DataGridView1.Rows
If ItemCodeStr = dgrow.Cells(2).Value.ToString Then
dgrow.DefaultCellStyle.BackColor = Color.Pink
MsgBox("There are no matches found.", MsgBoxStyle.Information, "Result")
Blue color or Selection
Blue color or Selection
I used LINQ here so you can now discard your
datatable that you are using for
It's either you do it thru run-time but I recommend just set it to the properties of the
datagridview at design-time. Set these properties:
DataGridView1.MultiSelect = False DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Then declare a variable outside of your search procedure. This will be used when the selection reached the end of the grid:
Dim reachedEnd As Boolean = False
Now for the procedure:
Dim CurrIndex As Integer = 0 If reachedEnd Then CurrIndex = -1 reachedEnd = False Else CurrIndex = DataGridView1.CurrentRow.Index End If Dim resultSet = From drow As DataGridViewRow In Me.DataGridView1.Rows _ Where (drow.Cells(2).Value.ToString.ToUpper Like "*" & txtSearch.Text.ToUpper & "*" _ Or drow.Cells(3).Value.ToString.ToUpper Like "*" & txtSearch.Text.ToUpper & "*") _ And (drow.Index > CurrIndex) _ Select drow.Index If resultSet.Count > 0 Then With DataGridView1 .Rows(resultSet.ToList(0)).Selected = True .CurrentCell = .Rows(resultSet.ToList(0)).Cells(0) End With Else MsgBox("There are no records found.", MsgBoxStyle.Information, "Result") reachedEnd = True End If
This will search for possible matches with your search criteria for
Description then if you click your Find button again, it will go to the next possible match. If it reached the end of the grid, it will start again the searching at the top.
That's all. It's much shorter and simpler now (thanks LINQ!).