Read also: Connecting VB.NET to MySQL Database using MySQL Connector
Next, we'll try to bind data from a database table to DataGridView.
I have an example table named kategori.
Query:
Table values:
For UI design, add DataGridView into a form then name it grdData.
Type code as below:
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class Form1
Dim conn As MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
conn = New MySqlConnection()
conn.ConnectionString = "server=localhost;user id=root;" & _
"password=;database=latihan"
Try
conn.Open()
SQL = "Select kodekategori, namakategori From kategori"
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
grdData.DataSource = myData
conn.Close()
Catch myerror As MySqlException
MessageBox.Show("Error: " & myerror.Message)
Finally
conn.Dispose()
End Try
End Sub
End Class
Try to run/debug application. Data will be displayed on datagridview.
See also the video:
Related topic:
To support you in understanding this topic, please read also these articles:
I divide this topic into 4 parts. Make sure you have read the previous article.
- How to connect VB.NET to MySQL database
- How to binding MySQL data to DataGridView
- How to binding MySQL data to ComboBox List
I divide this topic into 4 parts. Make sure you have read the previous article.
0 Comments