VB.NET Tutorial : how to create DataGridView AutoComplete for one Column and data value fill from MySQL database?
VB.NET tutorial - DataGridView AutoComplete can handle Value from database like MySQL database, MS Access or SQL Server. AutoCompleteCustomSource.Add(), AutoCompleteMode, and AutoCompleteSource method can handle Autocomplete TextBox in DataGridView.
Please read :
Just press "F5" key and tell me what happend
If you're still confused with this tutorial, you can watch Video tutorial above.
Download Full source code DataGridView AutoComplete
Please read :
How AutoComplete Work?
First, we will create a connection to a Database (i use MySQL database and ODBC connection), we will connect from project to our database and get all data from database to show into a gridview, after that we can create AutoComplete source for special columns in a datagridview.Create New Project (DataGridView AutoComplete)
Just open your visual studio and create new project rename it with "DataGridViewAutoComplete" and at the Form1.vb just Add one DataGridView to the project, and design look like this images :Source code DataGridView AutoComplete
Imports System.Data.Odbc ' we can using ODBC Connection
Public Class Form1
Dim Connect As OdbcConnection
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' create connection using ODBC connection
Connect = New OdbcConnection("DSN=db_penjualan;MultipleActiveResultSets=True")
' query to Database
Dim sql As String = "SELECT * FROM as_products"
' use dataadapter
Dim da As New OdbcDataAdapter(sql, Connect)
' use dataset
Dim ds As New DataSet
' open connection
Connect.Open()
da.Fill(ds, "as_products")
' set datasource and datamember for a datagridview
DataGridView1.DataSource = ds
DataGridView1.DataMember = "as_products"
' close dataadapter and connection
da.Dispose()
Connect.Close()
End Sub
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim tb As New TextBox
Dim dt As New DataTable
Dim cmd As New OdbcCommand
Connect.Open()
cmd.Connection = Connect
cmd.CommandText = "SELECT productName from as_products"
Dim da As New OdbcDataAdapter
da.SelectCommand = cmd
da.Fill(dt)
Dim r As DataRow
For Each r In dt.Rows
If TypeOf e.Control Is TextBox Then
tb = e.Control
tb.AutoCompleteCustomSource.Add(r.Item("productName").ToString)
tb.AutoCompleteMode = AutoCompleteMode.Suggest
tb.AutoCompleteSource = AutoCompleteSource.CustomSource
End If
Next
da.Dispose()
cmd.Dispose()
Connect.Close()
End Sub
End Class
Just press "F5" key and tell me what happend
If you're still confused with this tutorial, you can watch Video tutorial above.
Video tutorial DataGridView AutoComplete
Download Full source code DataGridView AutoComplete
COMMENTS