Membuat program aplikasi CRUD sepertinya wajib kita pelajari apalagi bagi pemula, karena semua aplikasi berurusan dengan Create, Read, Update dan Delete data, tapi semua ini memerlukan dukungan Database agar terhubung. Namun dalam artikel kali ini kita hanya membuat prototype sebagai latihan tools sederhana artinya kita akan membuat aplikasi ini tanpa database, hanya butuh satu buah DataGridview sebagai penyimpan data sementara.
1. Buka Microsoft Visual Studio Anda
2. Klik Create a New Project
3. Cari template Windows Form App (WinForms) dan klik templatenya
4. Klik Next
5. Ketik nama Project > Folder Peyimpanan
6. Klik Next
7. Pilih Framework .NET 6.0
8. Klik Create (tunggu...sampai Form1 muncul)
Buka jendela kode, muncul jendela kode dan bersihkan semua kode yang ada dengan CTRL+A Del, lalu Copy-Paste kode dibawah ini :
Public Class Form1
Dim dt As New DataTable()
Dim rowEditIndex As Integer = -1
Dim edit As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Tambahkan kolom
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Nama", GetType(String))
dt.Columns.Add("Kelamin", GetType(String))
dt.Columns.Add("No. Tlp", GetType(String))
dt.Columns.Add("Alamat", GetType(String))
' Set DataSource DataGridView
DataGridView1.DataSource = dt
ComboBoxKelamin.Items.Add("Laki-laki")
ComboBoxKelamin.Items.Add("Perempuan")
ComboBoxKelamin.SelectedIndex = 0
'warna zebra cross selang-seling
With DataGridView1
.RowsDefaultCellStyle.BackColor = Color.White
.AlternatingRowsDefaultCellStyle.BackColor = Color.Silver
'warna saat baris dipilih
.DefaultCellStyle.SelectionBackColor = Color.DodgerBlue
.DefaultCellStyle.SelectionForeColor = Color.White
End With
TextBoxID.Enabled = False
TextBoxNama.Focus()
edit = False
DataGridView1.AllowUserToAddRows = False
Call AturTombol()
TextBoxID.ReadOnly = True
TextBoxID.Enabled = False
End Sub
Private Sub ButtonTambah_Click(sender As Object, e As EventArgs) Handles ButtonTambah.Click
edit = False
' Validasi input
If ComboBoxKelamin.Text = "" Then
MessageBox.Show("Masukan Jenis Kelamin!")
ComboBoxKelamin.Focus()
Exit Sub
End If
If TextBoxNama.Text = "" Then
MessageBox.Show("Masukan Nama!")
TextBoxNama.Focus()
Exit Sub
End If
If TextBoxTlp.Text = "" Then
MessageBox.Show("Masukan Nomor Telepon!")
TextBoxTlp.Focus()
Exit Sub
End If
If TextBoxAlamat.Text = "" Then
MessageBox.Show("Masukan Alamat lengkap!")
TextBoxAlamat.Focus()
Exit Sub
End If
' Nomor urut otomatis
Dim nomorUrut As Integer = dt.Rows.Count + 1
' TAMBAH DATA (SATU KALI SAJA)
dt.Rows.Add(
nomorUrut,
TextBoxNama.Text,
ComboBoxKelamin.Text,
TextBoxTlp.Text,
TextBoxAlamat.Text
)
' Bersihkan input
ClearInput()
' Auto select baris terakhir
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.Rows.Count - 1).Selected = True
DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(0)
'focus qursor
TextBoxNama.Focus()
'atur ulang tombol
Call AturTombol()
End Sub
Private Sub ButtonPrev_Click(sender As Object, e As EventArgs) Handles ButtonPrev.Click
edit = False
If DataGridView1.Rows.Count = 0 Then Exit Sub
Dim idx As Integer = DataGridView1.CurrentRow.Index
If idx <= 0 Then
MessageBox.Show("Sudah di record pertama", "Informasi",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
PindahRecord(idx - 1)
End Sub
Private Sub ButtonNext_Click(sender As Object, e As EventArgs) Handles ButtonNext.Click
edit = False
If DataGridView1.Rows.Count = 0 Then Exit Sub
Dim idx As Integer = DataGridView1.CurrentRow.Index
If idx >= DataGridView1.Rows.Count - 1 Then
MessageBox.Show("Sudah di record terakhir", "Informasi",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
PindahRecord(idx + 1)
End Sub
Private Sub ButtonHapus_Click(sender As Object, e As EventArgs) Handles ButtonHapus.Click
If DataGridView1.CurrentRow Is Nothing _
OrElse DataGridView1.CurrentRow.Index < 0 _
OrElse DataGridView1.CurrentRow.IsNewRow Then
MessageBox.Show("Pilih data yang valid untuk dihapus!")
Exit Sub
End If
Dim nama As String = DataGridView1.CurrentRow.Cells("Nama").Value.ToString()
Dim konfirmasi As DialogResult = MessageBox.Show(
"Yakin ingin menghapus data """ & nama & """ ?",
"Konfirmasi Hapus",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question
)
If konfirmasi = DialogResult.OK Then
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
RefreshNomorUrut()
AturTombol()
End If
edit = False
End Sub
Sub ClearInput()
TextBoxID.Clear()
TextBoxNama.Clear()
TextBoxTlp.Clear()
TextBoxAlamat.Clear()
ComboBoxKelamin.SelectedIndex = 0
TextBoxID.Focus()
End Sub
Sub RefreshNomorUrut()
For i As Integer = 0 To dt.Rows.Count - 1
dt.Rows(i)("ID") = i + 1
Next
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellClick
If edit = True Then
ButtonTambah.Enabled = False
ButtonHapus.Enabled = False
ButtonFirst.Enabled = False
ButtonLast.Enabled = False
ButtonNext.Enabled = False
ButtonPrev.Enabled = False
If e.RowIndex < 0 Then Exit Sub
rowEditIndex = e.RowIndex
TextBoxID.Text = DataGridView1.Rows(e.RowIndex).Cells("ID").Value.ToString()
TextBoxNama.Text = DataGridView1.Rows(e.RowIndex).Cells("Nama").Value.ToString()
ComboBoxKelamin.Text = DataGridView1.Rows(e.RowIndex).Cells("Kelamin").Value.ToString()
TextBoxTlp.Text = DataGridView1.Rows(e.RowIndex).Cells("No. Tlp").Value.ToString()
TextBoxAlamat.Text = DataGridView1.Rows(e.RowIndex).Cells("Alamat").Value.ToString()
End If
End Sub
Private Sub ButtonEdit_Click(sender As Object, e As EventArgs) Handles ButtonEdit.Click
edit = True
If rowEditIndex = -1 Then
MessageBox.Show("Pilih data yang ingin diedit terlebih dahulu!")
Exit Sub
End If
' Update data di baris yang dipilih
dt.Rows(rowEditIndex)("Nama") = TextBoxNama.Text
dt.Rows(rowEditIndex)("Kelamin") = ComboBoxKelamin.Text
dt.Rows(rowEditIndex)("No. Tlp") = TextBoxTlp.Text
dt.Rows(rowEditIndex)("Alamat") = TextBoxAlamat.Text
MessageBox.Show("Data berhasil diubah")
ButtonTambah.Enabled = True
ButtonHapus.Enabled = True
ButtonFirst.Enabled = True
ButtonLast.Enabled = True
ButtonNext.Enabled = True
ButtonPrev.Enabled = True
edit = False
' Reset input & mode edit
ClearInput()
rowEditIndex = -1
End Sub
Private Sub ButtonFirst_Click(sender As Object, e As EventArgs) Handles ButtonFirst.Click
PindahRecord(0)
MsgBox("Sudah diawal Record", vbInformation, "Informasi")
edit = False
End Sub
Private Sub ButtonLast_Click(sender As Object, e As EventArgs) Handles ButtonLast.Click
PindahRecord(DataGridView1.Rows.Count - 1)
MsgBox("Sudah diakhir Record", vbInformation, "Informasi")
edit = False
End Sub
Private Sub PindahRecord(posisi As Integer)
If DataGridView1.Rows.Count = 0 Then Exit Sub
If posisi < 0 Then posisi = 0
If posisi > DataGridView1.Rows.Count - 1 Then
posisi = DataGridView1.Rows.Count - 1
End If
DataGridView1.ClearSelection()
DataGridView1.Rows(posisi).Selected = True
DataGridView1.CurrentCell = DataGridView1.Rows(posisi).Cells(0)
End Sub
Sub AturTombol()
Dim adaData As Boolean = (dt.Rows.Count > 0)
ButtonTambah.Enabled = True ' selalu aktif
ButtonEdit.Enabled = adaData
ButtonHapus.Enabled = adaData
ButtonPrev.Enabled = adaData
ButtonNext.Enabled = adaData
ButtonFirst.Enabled = adaData
ButtonLast.Enabled = adaData
End Sub
Private Sub TextBoxTlp_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxTlp.KeyPress
' Izinkan angka & Backspace
If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
End Class
Program ini tidak menggunakan Databse jika anda menutupnya maka akan hilang data yang Anda input
Itulah artikel hari ini tentang Membuat Program Aplikasi CRUD Tanpa DataBase Dengan DataGridView di Visual Basic .Net (VB.NET), semoga artikel ini bermanfaat buat kita semua.Amin.
Selamat mencoba semoga berhasil.
Post a Comment for "Membuat Program Aplikasi CRUD Tanpa DataBase Dengan DataGridView di Visual Basic .Net (VB.NET)"
Berkomentarlah dengan sopan sesuai tema yang Anda baca.
Jangan menyimpang dari tema