Paul Edward Pagente Paul Edward Pagente - 2 months ago 14
Vb.net Question

How to move into specific row/cell in datagridview using textbox vb.net

I have a program that looks like this.

enter image description here

If I click the button search on the top form, a new form will show like this.

enter image description here

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.

enter image description here

and this is the code for that (Given by the one of the best programmer that I know)

Try
If TextBox1.Text = "" Then
MsgBox("Nothing to search.")
Else
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
End If
Next
Next
Else
MsgBox("There are no matches found.", MsgBoxStyle.Information, "Result")
End If
End If
Catch
End Try


Now my question is how can i make the search a selection instead of coloring the data?

I mean this one.

enter image description here

If my type a word in textbox the
Blue color or Selection
will go there. If the data is in the bottom then the
Blue color or Selection
will go to the bottom something like that.

TYSM for future help

Answer

I used LINQ here so you can now discard your datatable that you are using for .Select() function

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 ItemCode and 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!).

Comments