VB.NET Ms Access: Menampilkan Data Di DataGridView


Sekarang kita akan membahas bagaimana menampilkan data dari table di Ms Access ke DataGridView.
Bentuk tabel yang kita gunakan:
UI yang akan digunakan hanya form dan sebuah DataGridView.
Code VB.NET:
Imports System.Data.OleDb

Public Class Form1
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0" & _
                                   ";Data Source=latihan.accdb;" & _
                                   "Persist Security Info=False;")

    Private Sub Form1_Load(sender As Object, _
                e
As EventArgs) Handles MyBase.Load
        Dim cmd As New OleDbCommand
       
Dim adapt As New OleDbDataAdapter
       
Dim dt As New DataTable

        Try
            con.Open()

            cmd.Connection = con
            cmd.CommandText = "SELECT kodekategori, namakategori " & _
                              "FROM kategori"

            adapt.SelectCommand = cmd
            adapt.Fill(dt)

            DataGridView1.DataSource = dt

            con.Close()
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
            con.Dispose()
        End Try
    End Sub

End Class


Hasil run:



Click here if you like this article.


Post a Comment

0 Comments