Filter Columns Value in DataGridView - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

In this tutorial, we will filter the Item Code column based on the value in TextBox1

Visual Basic .NET Filter DataGridView

First add the following namespaces:

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable

Then add the following declaration after Public Class Form1

Public Class Form1
    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
    Dim ds As DataSet = New DataSet
    Dim da As OleDbDataAdapter
    Dim tables As DataTableCollection = ds.Tables
    Dim source1 As New BindingSource()

the following code is the form load event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        dataFile = "C:\Users\Jimmy\Documents\Merchandise.accdb" ' change to access database location on your computer
        connString = provider & dataFile
        myConnection.ConnectionString = connString
        da = New OleDbDataAdapter("Select [Item Code], [Description], [Price] from items", myConnection)
        da.Fill(ds, "Items")
        ' replace "items" with the name of the table
        ' replace [Item Code], [Description], [Price] with the columns headers
        Dim view1 As New DataView(tables(0))
        source1.DataSource = view1
        DataGridView1.DataSource = view1
End Sub

Filter button click event

Private Sub FilterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FilterButton.Click
        source1.Filter = "[Item Code] = '" & TextBox1.Text & "'"
End Sub


filter datagridview in

Share This

Home | About | Contact | Privacy Policy

Copyright 2017 - All Rights Reserved.