Visual Basic .Net : Search in Access Database - DataGridView BindingSource Filter

👨‍🏫 Please watch the top 3 most popular videos on my channel.

🥇 33 Million Views : Programming in Visual Basic .Net How to Connect Access Database to VB.NET : https://youtu.be/cwDqjmSmtMQ

🥈 13 Million Views : Visual Basic .Net : Search in Access Database - DataGridView BindingSource Filter (Part 1/2) : https://youtu.be/UoT2oava9ns

🥉 2 Million Views : Visual Basic .Net : Search in Access Database - DataGridView BindingSource Filter (Part 2/2) : https://youtu.be/e5Dvkw7moWg


🚀 Visual Studio 2010 : VB.NET 2010 Programming - Search in Access 2007 Database

Filtering Data In DataGridView Using Textbox example by iBasskung (DataGridView BindingSource Filter).

Visual Basic .Net : Search in Access Database - DataGridView BindingSource Filter
Part 1/2


Visual Basic .Net : Search in Access Database - DataGridView BindingSource Filter
Part 2/2



Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Your_DatabaseDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.Your_DatabaseDataSet.Table1)

        With DataGridView1
            .ClearSelection()
            .ReadOnly = True
            .MultiSelect = False
        End With

        Dim colors() As String = [Enum].GetNames(GetType(KnownColor))

        cboDfColor.Items.AddRange(colors)
        cboAltColor.Items.AddRange(colors)

        Me.reset()

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Table1BindingSource.MovePrevious()
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        Table1BindingSource.AddNew()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Table1BindingSource.MoveNext()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        On Error GoTo SaveErr
        Table1BindingSource.EndEdit()
        Table1TableAdapter.Update(Your_DatabaseDataSet.Table1)

ErrEx:
        Exit Sub
SaveErr:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrEx

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If MessageBox.Show("Are you sure", "Delete Record", MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = _
                       Windows.Forms.DialogResult.Yes Then
            Table1BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

        Dim msg As String = "Do you want to Exit?"
        Dim title As String = "Exit Application"

        If MessageBox.Show(msg, title, MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = _
                       Windows.Forms.DialogResult.Yes Then
            Me.Close() 'End
        End If

    End Sub
    
    Private Sub cboDfColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDfColor.SelectedIndexChanged
        cboDfColor.BackColor = Color.FromName(cboDfColor.SelectedItem)
        txtSearch.Select()
    End Sub

    Private Sub cboAltColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAltColor.SelectedIndexChanged
        cboAltColor.BackColor = Color.FromName(cboAltColor.SelectedItem)
        txtSearch.Select()
    End Sub

    Private Sub lblReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblReset.Click
        On Error GoTo ErrRe
        txtSearch.Select()
        Table1BindingSource.Filter = Nothing


        With DataGridView1
            .ClearSelection()
            .ReadOnly = True
            .MultiSelect = False
            .DataSource = Table1BindingSource
        End With

        Me.reset()

ErrEx:
        Exit Sub
ErrRe:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrEx
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        On Error GoTo SearchErr

        If txtSearch.Text = "" Then
            Call notFound()
            Exit Sub

        Else

            Dim cantFind As String = txtSearch.Text
            Me.dgvFill()

            Table1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & txtSearch.Text & "')" & _
                "OR (YourName LIKE '" & txtSearch.Text & "') OR (Lastname LIKE '" & txtSearch.Text & "')" & _
                "OR (Phone LIKE '" & txtSearch.Text & "') OR (Email LIKE '" & txtSearch.Text & "')" & _
                "OR (Facebook LIKE '" & txtSearch.Text & "')"

            If Table1BindingSource.Count <> 0 Then
                With DataGridView1
                    .DataSource = Table1BindingSource
                End With

            Else

                Me.notFound()

                MsgBox("--> " & cantFind & vbNewLine & _
                       "The search item was not found.", _
                       MsgBoxStyle.Information, "Hey Boss!")

                Table1BindingSource.Filter = Nothing

                With DataGridView1
                    .ClearSelection()
                    .ReadOnly = True
                    .MultiSelect = False
                    .DataSource = Table1BindingSource
                End With

            End If

        End If

ErrExit:
        Exit Sub
SearchErr:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrExit

    End Sub


    Private Sub reset()

        Dim txtS As TextBox = txtSearch
        With txtS
            .Text = ""
            .Select()
            .BackColor = Color.LightCyan
        End With

        cboDfColor.SelectedItem = "MistyRose"
        cboAltColor.SelectedItem = "Gold"

        cboDfColor.BackColor = Color.LightCyan
        cboAltColor.BackColor = Color.LightCyan

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If


        Dim dgv1 As DataGridView = DataGridView1
        With dgv1
            .RowsDefaultCellStyle.BackColor = Color.FromName(cboDfColor.SelectedItem)
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FromName(cboAltColor.SelectedItem)
        End With

    End Sub


    Private Sub dgvFill()

        txtSearch.BackColor = Color.LightBlue

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If


        Dim myDGV1 As DataGridView = DataGridView1
        With myDGV1
            .RowsDefaultCellStyle.BackColor = Color.FromName(cboDfColor.SelectedItem)
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FromName(cboAltColor.SelectedItem)
        End With

    End Sub

    Private Sub notFound()

        Dim txtS As TextBox = txtSearch
        With txtS
            .BackColor = Color.White
            .Select()
            .SelectAll()
        End With

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If


        Dim dgv As DataGridView = DataGridView1
        With dgv
            .RowsDefaultCellStyle.BackColor = Color.White
            .AlternatingRowsDefaultCellStyle.BackColor = Color.White
        End With
    End Sub

End Class


🎯 You can find me on the other social media platforms:

📺 Amazon:

📍 https://www.amazon.com/shop/ibasskung

📺 Pages:

📍 https://www.facebook.com/CodeAMinute

📍 https://www.facebook.com/IbasskungTutorial

📍 https://www.facebook.com/codewithibasskung

📍 https://www.facebook.com/iBasskungAcademy

📺 YouTube:

📍 https://www.youtube.com/c/iBasskung

📺 Udemy:

📍 https://www.udemy.com/user/tanin-sangngam

📺 Twitter:

📍 https://twitter.com/IBasskung

📺 Pinterest:

📍 https://www.pinterest.com/ibasskung

📺 Blogger:

📍 .NET: https://codeaminute.blogspot.com

📍 JAVA: https://javacodeminutes.blogspot.com

📍 VBA: https://vbacodeminutes.blogspot.com

📺 WordPress: 

📍 http://myvbdotnet.wordpress.com


💯 THANK YOU SO MUCH 💯


#VisualBasic #CSharp #iBasskung 

Comments

  1. Tecocraft is the most valuable iOS, iPhone & iPad app development service in the US providers that build a high-quality app that helps you grow your online business exponentially

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment