cara membuat autocomplete datagridview


Imports System.Data.OleDb

 

Public Class AutocompleteDGV

 

Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader

 

Sub Koneksi()
conn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=dbpersediaan.mdb")
conn.Open()
End Sub

 

Private Sub AutocompleteDatagrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Call Koneksi()
DGV.Columns.Add("Nama", "Nama Barang")
DGV.Columns(0).Width = 440
DGV.Columns.Add("Jumlah", "Jumlah")
End Sub

 

Private Sub dgv_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGV.EditingControlShowing
Dim titleText As String = DGV.Columns(0).HeaderText
If titleText.Equals("Nama Barang") Then
Dim autoText As TextBox = TryCast(e.Control, TextBox)
If autoText IsNot Nothing Then
autoText.AutoCompleteMode = AutoCompleteMode.Suggest
autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim DataCollection As New AutoCompleteStringCollection()
addItems(DataCollection)
autoText.AutoCompleteCustomSource = DataCollection
End If
End If
End Sub

 

Public Sub addItems(ByVal col As AutoCompleteStringCollection)
Call Koneksi()
cmd = New OleDbCommand("select * from barang", conn)
dr = cmd.ExecuteReader
Do While dr.Read
col.Add(dr("namabrg"))
Loop
End Sub

 

Private Sub DGV_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellEndEdit
If e.ColumnIndex = 0 Then
cmd = New OleDbCommand("select * from barang where namabrg='" & DGV.Rows(e.RowIndex).Cells(0).Value & "'", Conn)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
DGV.Rows(e.RowIndex).Cells(1).Value = dr("jumlahbrg")
End If
End If
End Sub

 

Private Sub DGV_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGV.KeyDown
If e.KeyCode = Keys.Escape Then
End
End If
End Sub

 

End Class