Dalam tutorial
ini, kita akan mencari/menyaring data pada datagridview berdasarkan nilai di
TextBox1.
Bukalah contoh Form
yang kita buat sebelumnya lalu, tambahkan TextBox dan Button pada form seperti
tampilan berikut :
Pertama buat ruang nama berikut:
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Kemudian tambahkan deklarasi berikut setelah 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()
Masukkan kode berikut pada Form1_ load:
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")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Masukkan kode berikut pada button_clik:
Private Sub FilterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FilterButton.Click
source1.Filter = "[Item Code] = '" & TextBox1.Text & "'"
DataGridView1.Refresh()
End Sub
Jalankan Applikasi nya, klik button maka hasilnya akan terlihat seperti tampilan berikut ;
Pelajari Juga
Tutorial Visual Basic Net 2013 berikut ini ;
Salam...,
No comments:
Post a Comment