Senin, 11 Juli 2016

Cara Menampilkan Record Database ke DataGridView Visual Basic

Berikut ini tutorial bagaimana cara menampilkan record database menggunakan item DataGridView di Visual Basic VB.NET. Untuk membuatnya anda membutuhkan class dan sebuah form, agar lebih jelas anda dapat mendownload project maupun melihat video panduan pembuatannya.
Kode Koneksi:

Imports MySql.Data.MySqlClient
Public Class koneksidata
    Dim conect As New MySqlConnection("server=localhost;uid=root;pwd='';database='dbgrid'")
    Public Function open() As MySqlConnection
        Try
            conect.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return conect
    End Function
    Public Function close() As MySqlConnection
        Try
            conect.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return conect
    End Function
End Class
Kode Form1:

Imports MySql.Data.MySqlClient
Public Class Form1
#Region "deklarasi"
    Dim mycmd As New MySqlCommand
    Dim myconection As New koneksidata
    Dim dadapter As MySqlDataAdapter
    Dim dtable As New DataTable
#End Region
#Region "prosedur"
    Sub tampilkangrid()
        mycmd.Connection = myconection.open
        mycmd.CommandText = "select * from tbgrid order by id asc"
        dadapter = New MySqlDataAdapter(mycmd)
        dtable.Rows.Clear()
        dadapter.Fill(dtable)
        If Not dtable Is Nothing AndAlso dtable.Rows.Count > 0 Then
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = dtable
            DataGridView1.Columns(0).DataPropertyName = "id"
            DataGridView1.Columns(1).DataPropertyName = "nama"
        End If
        'With DataGridView1
        '    .Columns(0).HeaderText = "ID"
        '    .Columns(1).HeaderText = "Nama"
        'End With
        myconection.close()
    End Sub
    Sub klikgrid()
        Dim i As Integer
        Try
            With DataGridView1
                If .CurrentCell.Value Is Nothing Then
                    Exit Sub
                Else
                    i = .CurrentRow.Index
                    TID.Text = .Rows(i).Cells(0).Value.ToString
                    TNAMA.Text = .Rows(i).Cells(1).Value.ToString
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
    End Sub
#End Region
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        mycmd.Connection = myconection.open
        mycmd.CommandText = "insert into tbgrid(id,nama) values('" & TID.Text & "','" & TNAMA.Text & "')"
        mycmd.ExecuteNonQuery()
        myconection.close()
        tampilkangrid()
        MsgBox("Data disimpan !", MsgBoxStyle.Information, "Pemberitahuan !")
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        mycmd.Connection = myconection.open
        mycmd.CommandText = "update tbgrid set nama='" & TNAMA.Text & "' where id='" & TID.Text & "'"
        mycmd.ExecuteNonQuery()
        myconection.close()
        tampilkangrid()
        MsgBox("Data dirubah !", MsgBoxStyle.Information, "Pemberitahuan !")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        mycmd.Connection = myconection.open
        mycmd.CommandText = "delete from tbgrid where id='" & TID.Text & "'"
        mycmd.ExecuteNonQuery()
        myconection.close()
        tampilkangrid()
        MsgBox("Data dihapus !", MsgBoxStyle.Information, "Pemberitahuan !")
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        tampilkangrid()
    End Sub
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        klikgrid()
    End Sub
End Class

Download Project:
Download Project
Lihat Video:

0 komentar:

Posting Komentar

Kita bukanlah apa-apa tanpaNya, jika ada pertanyaan mengenai artikel silakan tinggalkan komentar anda melalui form komentar :)

SkyArs Corp. © 2016. Tweaked by: Soni Sitez Powered by: Blogger