14. Tambahkan Form baru dengan nama frmListProdi.
vb, Desain dengan layout
sbb :
Obyek Properties Nilai
FileName frmListProdi.vb
Name frmListProdi
BackColor Pilih warna yg diinginkan
ForeColor Pilih warna yg diinginkan
Form
MaximizeBox False
MinimizeBox False
StartPosition CenterScreen
Text Daftar Data Program Studi
DataGridView Name DGV
Button1 Name btnTambah
Text Tambah
Name btnTutup
Button2
Text Tutup
Pemrograman Visual II by Amiruddin, M.Kom 2021 45 | P a g e
Lengkapi Codiong sbb :
Imports System.Data.Odbc
Public Class frmListProdi
Sub Tampilkandata()
Dim btnEdit As New DataGridViewButtonColumn()
Dim btnHapus As New DataGridViewButtonColumn()
DGV.Columns.Clear()
DGV.Columns.Add("kol0", "Kode Prodi")
DGV.Columns.Add("kol1", "Program Studi")
DGV.Columns.Add("kol2", "Jenjang")
Dim sql As String = "Select kode_prodi,nama_prodi,jenjang " & _
" from tbprogram_studi order by kode_prodi"
cmd = New OdbcCommand(sql, Conn)
rd = cmd.ExecuteReader
Dim i As Integer = 0
While rd.Read
DGV.Rows.Add()
DGV.Item(0, i).Value = rd("kode_prodi")
DGV.Item(1, i).Value = rd("nama_prodi")
DGV.Item(2, i).Value = rd("jenjang")
i += 1
End While
rd.Close()
DGV.ReadOnly = True
DGV.AllowUserToAddRows = False
DGV.Columns(0).Width = 80
DGV.Columns(1).Width = 200
DGV.Columns(2).Width = 80
DGV.GridColor = Color.Blue
DGV.DefaultCellStyle.ForeColor = Color.Black
DGV.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DGV.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DGV.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DGV.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DGV.Columns().Add(btnEdit)
btnEdit.HeaderText = ""
btnEdit.Name = "btEdit"
btnEdit.Text = "Edit"
btnEdit.DefaultCellStyle.ForeColor = Color.Maroon
btnEdit.UseColumnTextForButtonValue = True
DGV.Columns().Add(btnHapus)
btnHapus.HeaderText = ""
btnHapus.Name = "btHapus"
btnHapus.Text = "Hapus"
Pemrograman Visual II by Amiruddin, M.Kom 2021 46 | P a g e
btnHapus.DefaultCellStyle.ForeColor = Color.Maroon
btnHapus.UseColumnTextForButtonValue = True
DGV.Columns(3).Width = 60
DGV.Columns(4).Width = 60
End Sub
Private Sub frmListProdi_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
Tampilkandata()
End Sub
Private Sub frmListProdi_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
SendKeys.Send("{tab}")
End If
End Sub
Private Sub frmListProdi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Call Tampilkandata()
End Sub
Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick
If e.ColumnIndex = 3 Then
tblEdit = True
frmProg_Studi.Text = "Edit Data Program Studi"
cKode_Prodi = DGV.SelectedRows.Item(0).Cells(0).Value()
frmProg_Studi.Show()
End If
If e.ColumnIndex = 4 Then
cKode_Prodi = DGV.SelectedRows.Item(0).Cells(0).Value()
If MessageBox.Show("Yakin akan dihapus..?", "Hapus Data", MessageBoxButtons.YesNo) =
Windows.Forms.DialogResult.Yes Then
cmd = New OdbcCommand("Delete from tbprogram_studi where kode_prodi='" & cKode_Prodi & "'",
Conn)
cmd.ExecuteNonQuery()
Tampilkandata()
End If
End If
End Sub
Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnTutup.Click
Me.Close()
End Sub
Pemrograman Visual II by Amiruddin, M.Kom 2021 47 | P a g e
Private Sub btnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnTambah.Click
tblEdit = False
frmProg_Studi.Text = "Tambah Data Program Studi"
frmProg_Studi.Show()
End Sub
End Class
12.Tambahkan Form Baru dengan nama frmEntryProdi.vb, Desain dengan
layout sbb :
Obyek Properties Nilai
FileName frmEntryProdi.vb
Name frmEntryProdi
BackColor Pilih warna yg diinginkan
ForeColor Pilih warna yg diinginkan
Form
MaximizeBox False
MinimizeBox False
StartPosition CenterScreen
Text Entry Data Prodi
Label Text Isi sesuai dgn judulnya
TextBox1 Name txtKode
TextBox2 Name txtProdi
ComboBox1 Name cboJenjang
Name btnSimpan
Button1
Text Simpan
Name btnBatal
Button2
Text Batal
Pemrograman Visual II by Amiruddin, M.Kom 2021 48 | P a g e
Lengkapi Codiong sbb :
Imports System.Data.Odbc
Public Class frmEntryProdi
Sub TampilJenjang()
cboJenjang.Items.Clear()
cmd = New OdbcCommand("select distinct jenjang from tbprogram_studi order by kode_prodi", Conn)
rd = cmd.ExecuteReader
While rd.Read
cboJenjang.Items.Add(rd.Item(0))
End While
End Sub
Sub kosongkan()
txtKode.Text = ""
txtProdi.Text = ""
cboJenjang.Text = ""
txtKode.Enabled = True
txtKode.Focus()
End Sub
Sub CekData()
cmd = New OdbcCommand("select * from tbprogram_studi where kod_prodi='" & txtKode.Text & "' ", Conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows = True Then
txtKode.Text = rd.Item("kod_prodi")
txtProdi.Text = rd.Item("nama_prodi")
cboJenjang.Text = rd.Item("jenjang")
cId_Periode = rd.Item("kode_fak")
txtKode.Enabled = False
Else
txtProdi.Text = ""
cboJenjang.Text = ""
End If
End Sub
Sub Validasi()
If txtProdi.Text = "" Then
MsgBox("Nama Prodi Belum diisi", , "Perhatian...!")
txtProdi.Focus()
Valid = False
Exit Sub
Else
Valid = True
End If
If cboJenjang.Text = "" Then
MsgBox("Jenjang Program Studi Belum diisi", , "Perhatian...!")
Pemrograman Visual II by Amiruddin, M.Kom 2021 49 | P a g e
cboJenjang.Focus()
valid = False
Exit Sub
Else
valid = True
End If
End Sub
Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnSimpan.Click
Call Validasi()
If valid = True Then
cmd = New OdbcCommand("Select * from tbprogram_studi where kode_prodi='" & txtKode.Text & "' ",
Conn)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
Dim sqltambah As String = "Insert into tbprogram_studi (kode_prodi,nama_prodi,jenjang) values " & _
"('" & txtKode.Text & "','" & txtProdi.Text & "', " & _
"'" & cboJenjang.Text & "')"
cmd = New OdbcCommand(sqltambah, Conn)
Else
Dim sqledit As String = "Update tbprogram_studi set " & _
"nama_prodi='" & txtProdi.Text & "', " & _
"jenjang='" & cboJenjang.Text & "' " & _
" where kode_prodi='" & txtKode.Text & "' "
cmd = New OdbcCommand(sqledit, Conn)
End If
cmd.ExecuteNonQuery()
Me.Close()
Me.Visible = False
frmListProdi.Tampilkandata()
End If
End Sub
Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
Me.Close()
Me.Visible = False
frmListProdi.Tampilkandata()
End Sub
Private Sub frmEntryProdi_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
SendKeys.Send("{tab}")
End If
End Sub
Pemrograman Visual II by Amiruddin, M.Kom 2021 50 | P a g e
Private Sub frmEntryProdi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Call TampilJenjang()
If tblEdit = False Then
Call kosongkan()
Else
txtKode.Enabled = False
cmd = New OdbcCommand("select * from tbprogram_studi where kode_prodi='" & cKode_Prodi & "' ",
Conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows = True Then
txtKode.Text = rd.Item("kode_prodi")
txtProdi.Text = rd.Item("nama_prodi")
cboJenjang.Text = rd.Item("jenjang")
End If
End If
End Sub
End Class
Pemrograman Visual II by Amiruddin, M.Kom 2021 51 | P a g e