Binding navigator and binding source go to a specific ID

Claude Larocque 666 Reputation points
2022-06-02T16:51:36.577+00:00

Hi everyone,
I have a form name FrmEditProducts on which I have fields and a datagridview. I have created 2 filter textboxes one to search by ID and the other by name. I would like the Binding navigator and the binding source to select that ID and display the information for that product or name. At the moment even if the fields and datagridview retrieve that right information, I would like the navigator and the source to select that ID. I have included 2 images showing the before and after the filter and the code on the concerned fields.

    Private Sub TxtFilterIDTB_TextChanged(sender As Object, e As EventArgs) Handles TxtFilterIDTB.TextChanged  
        Try  
            If TxtFilterIDTB.Text = "" Then  
                Showdata()  
            Else  
                SearchID()  
            End If  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Private Sub TxtFilterIDTB_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtFilterIDTB.KeyPress  
        Try  
            Dim ch As Char = e.KeyChar  
            If Not Char.IsDigit(ch) AndAlso Asc(ch) <> 8 Then  
                e.Handled = True  
            End If  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Private Sub TxtFilterIDTB_Enter(sender As Object, e As EventArgs) Handles TxtFilterIDTB.Enter  
        Try  
            Me.TxtFilterNameTB.Text = ""  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Public Sub Showdata()  
        Try  
            Dim constr As String = SQL.DBCon.ConnectionString  
            Dim DBCon As New SqlConnection(constr)  
            Dim query As String = "SELECT * FROM Warehouse.Products"  
            Dim DBCmd As New SqlCommand(query, DBCon)  
            Dim DBDA As New SqlDataAdapter(DBCmd)  
            Dim DBDT As New DataTable()  
            DBDA.Fill(DBDT)  
            ProductsDGV.DataSource = DBDT  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Public Sub SearchID()  
        Try  
            Dim constr As String = SQL.DBCon.ConnectionString  
            Dim DBCon As New SqlConnection(constr)  
            Dim query As String = "SELECT * from Warehouse.Products where ProductID='" & TxtFilterIDTB.Text & "'"  
            Dim DBCmd As New SqlCommand(query, DBCon)  
            Dim DBDA As New SqlDataAdapter(DBCmd)  
            Dim DBDT As New DataTable()  
            DBDA.Fill(DBDT)  
            ProductsDGV.DataSource = DBDT  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Public Sub SearchName()  
        Try  
            Dim constr As String = SQL.DBCon.ConnectionString  
            Dim DBCon As New SqlConnection(constr)  
            Dim query As String = "SELECT * from Warehouse.Products where CONCAT(ProductName, Category, Subcategory, Department, SeasonCode) Like '%" & TxtFilterNameTB.Text & "%'"  
            Dim DBCmd As New SqlCommand(query, DBCon)  
            Dim DBDA As New SqlDataAdapter(DBCmd)  
            Dim DBDT As New DataTable()  
            DBDA.Fill(DBDT)  
            ProductsDGV.DataSource = DBDT  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Private Sub BtnResetSearchByName_Click(sender As Object, e As EventArgs) Handles BtnResetSearchByName.Click  
        Try  
            Me.TxtFilterNameTB.Text = ""  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Private Sub TxtFilterNameTB_TextChanged(sender As Object, e As EventArgs) Handles TxtFilterNameTB.TextChanged  
        Try  
            If TxtFilterNameTB.Text = "" Then  
                Showdata()  
                BtnSave.Enabled = True  
                BtnDelete.Enabled = True  
                BtnAdd.Enabled = True  
                ProductNameTB.Enabled = True  
            Else  
                SearchName()  
                BtnSave.Enabled = False  
                BtnDelete.Enabled = False  
                BtnAdd.Enabled = False  
                ProductNameTB.Enabled = False  
            End If  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
    Private Sub TxtFilterNameTB_Enter(sender As Object, e As EventArgs) Handles TxtFilterNameTB.Enter  
        Try  
            Me.TxtFilterIDTB.Text = ""  
            If SQL.HasException(True) Then Exit Sub  
        Catch ex As Exception  
            MsgBox(ex.Message)  
            System.IO.File.AppendAllText("C:\AutoCashRegister\Documents\log.txt", ex.ToString & vbNewLine & vbNewLine)  
        End Try  
    End Sub  
  
  
I HAVE TRY THIS WITHOUT SUCCESS:  
  
    Public Sub ChangeBSCurrentPosition()  
        Dim dgv As DataGridView = ProductsDGV  
        If ProductsDGV.CurrentRow IsNot Nothing Then  
            Dim index As Integer = Convert.ToString(dgv.CurrentRow.Cells(0).Value)  
            If index < -1 Then  
                ProductsBS.Position = index  
            End If  
        End If  
    End Sub  
Developer technologies VB
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.