Ok, sorry rada telat postingnya.. kemarin sempat ada proyek n juga beberapa hari kemarin lagi sakit.
Langsung aja, di
tutorial ke-2
kemarin kita telah sedikit mengenal tentang LINQ dan juga telah membuat
suatu form barang. Kali ini kita akan mencoba menambahkan kode-kode
yang akan diproses oleh tiap tombol dalam form barang tersebut. Untuk
kali ini validasi dalam setiap proses akan diabaikan terlebih dahulu,
nanti pasti menyusul…
Sebagai tambahan, lebih tepatnya “update”,
pada tiap tabel dalam database semua yang bertype nchar di ubah ke
nvarchar.. tanya kenapa??
Sebelum dilanjutkan, ada baiknya semua kode-kode dalam form barang di hapus dahulu,
kecuali, kode-kode di bawah ini:
- Public Class frmBarang
- Private Sub frmBarang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- End Sub
- End Class
1. Dekarasi beberapa variabel umum
Masukkan kode berikut pada bagian atas setelah
Public Class frmBarang :
- Private db As New DataClasses1DataContext
- Private edit As Boolean
1.a. Sub Procedure buka
Digunakan untuk mengunci dan membuka TextBox dan ComboBox dalam frmBarang:
- Private Sub buka(ByVal t As Boolean)
- Dim daftarTextBox = From myControl In Me.Controls _
- Where TypeOf myControl Is TextBox _
- Select myControl
- For Each myTeksBoks As TextBox In daftarTextBox
- If t Then
- myTeksBoks.Enabled = True
- Me.cmbKodeKategori.Enabled = True
- Else
- myTeksBoks.Enabled = False
- Me.cmbKodeKategori.Enabled = False
- End If
- Next
- End Sub
1.b. Sub Procedure isiCombo
Digunakan untuk mengisi daftar kategori untuk ComboBox pada frmBarang:
- Private Sub isiCombo()
- Me.cmbKodeKategori.ResetText()
- Dim cmbKate = From kategori In db.kategoris _
- Select kategori.NmKategori
- For Each kate As String In cmbKate
- Me.cmbKodeKategori.Items.Add(kate)
- Next
- End Sub
1.c. Sub Procedure resData
Digunakan untuk me-refresh (mengisi) data dalam DataGridView1 dengan data yang baru (Kodenya s
edikit berbeda dari tutorial 2 kemarin):
- Private Sub resData()
- Dim qBarang = From barang In db.barangs _
- Join kategori In db.kategoris _
- On kategori.KDKategori Equals barang.KDKategori _
- Select Kode = barang.KDBarang, Nama = barang.NmBarang, _
- Kategori = kategori.NmKategori, Jumlah = barang.Jumlah, Harga = barang.HargaJual
- Me.DataGridView1.DataSource = qBarang
- End Sub
1.d. Sub Procedure dgKlik
Digunakan untuk mengisi tiap TextBox dan ComboBox berdasarkan data yang
ada dalam DataGridView1 saat setiap kali data dalam DataGridView1
dipilih.
- Private Sub dgKlik()
- Try
- Me.txtKodeBarang.Text = Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value
- Me.txtNamaBarang.Text = Me.DataGridView1.Item(1, Me.DataGridView1.CurrentRow.Index).Value
- Me.txtJumlah.Text = Me.DataGridView1.Item(3, Me.DataGridView1.CurrentRow.Index).Value
- Me.txtHarga.Text = Me.DataGridView1.Item(4, Me.DataGridView1.CurrentRow.Index).Value
- Dim dkate = From kategori In db.kategoris _
- Where kategori.NmKategori Is Me.DataGridView1.Item(2, Me.DataGridView1.CurrentRow.Index).Value _
- Select kategori.NmKategori
- Me.cmbKodeKategori.Text = dkate.Single
- Catch ex As Exception
- MessageBox.Show(“Error: ” & ex.Message)
- End Try
- End Sub
1.e. Sub Procedure resTeks
Digunakan untuk mengosongkan semua TextBox dan Combobox dalam frmBarang :
- Private Sub resTeks()
- isiCombo()
- Dim daftarTextBox = From myControl In Me.Controls _
- Where TypeOf myControl Is TextBox _
- Select myControl
- For Each myTeksBoks As TextBox In daftarTextBox
- myTeksBoks.Text = “”
- Next
- End Sub
1.f. Function katPerNama
Digunakan untuk mengambil kode Kategori barang berdasarkan nama kategorinya :
- Private Function katPerNama(ByVal n As String) As Integer
- Dim kat As Integer = 0
- Dim kate = From kategori In db.kategoris _
- Where kategori.NmKategori Is n _
- Select kategori.KDKategori
- kat = kate.Single
- Return kat
- End Function
2. DataGridView1_Click
Pada designer view, double klik Control DataGridView1 > akan masuk ke
dalam tampilan Code View. Pada bagian Method Name, pilih Click. Lihat
Gambar :
Ketikkan kode berikut pada bagian DataGridView1_Click :
- Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
- dgKlik()
- End Sub
3. Sub Procedure Saat Tombol Edit Diklik
Double klik tombol edit dan masukkan kode berikut :
- Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
- Me.cmdBaru.Text = “Batal”
- Me.cmdEdit.Enabled = False
- Me.cmdHapus.Enabled = False
- Me.cmdSimpan.Enabled = True
- edit = True
- buka(True)
- Me.txtKodeBarang.Enabled = False
- Me.txtNamaBarang.Focus()
- End Sub
4. Sub Procedure Saat Tombol Baru Diklik
Double klik tombol baru dan masukkan kode berikut :
- Private Sub cmdBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBaru.Click
- If Me.cmdBaru.Text = “Baru” Then
- Me.cmdBaru.Text = “Batal”
- Me.resTeks()
- buka(True)
- Me.DataGridView1.Enabled = False
- Me.cmdSimpan.Enabled = True
- Me.cmdEdit.Enabled = False
- Me.cmdHapus.Enabled = False
- Me.cmdKeluar.Enabled = False
- Me.txtKodeBarang.Focus()
- Else
- Me.cmdBaru.Text = “Baru”
- buka(False)
- Me.DataGridView1.Enabled = True
- edit = False
- Me.cmdSimpan.Enabled = False
- Me.cmdEdit.Enabled = True
- Me.cmdHapus.Enabled = True
- Me.cmdKeluar.Enabled = True
- dgKlik()
- End If
- End Sub
5. Sub Procedure Saat Tombol Simpan Diklik
Double klik tombol simpan dan masukkan kode berikut :
- Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSimpan.Click
-
- If edit Then
- Dim updatebrg = (From barang In db.barangs _
- Where barang.KDBarang = Me.txtKodeBarang.Text).ToList()(0)
- With updatebrg
- .NmBarang = Me.txtNamaBarang.Text
- .KDKategori = katPerNama(Me.cmbKodeKategori.Text)
- .Jumlah = Me.txtJumlah.Text
- .HargaJual = Me.txtHarga.Text
- End With
- Else
- Dim brg As New barang With { _
- .KDBarang = Me.txtKodeBarang.Text, _
- .NmBarang = Me.txtNamaBarang.Text, _
- .KDKategori = katPerNama(Me.cmbKodeKategori.Text), _
- .Jumlah = Me.txtJumlah.Text, _
- .HargaJual = Me.txtHarga.Text}
- End If
- Try
- db.SubmitChanges()
- Catch ex As Exception
- MessageBox.Show(“Error :” & ex.Message)
- End Try
- resData()
- dgKlik()
- buka(False)
- Me.cmdSimpan.Enabled = False
- Me.cmdBaru.Text = “Baru”
- Me.cmdEdit.Enabled = True
- Me.cmdKeluar.Enabled = True
- edit = False
- Me.cmdHapus.Enabled = True
- Me.DataGridView1.Enabled = True
- End Sub
6. Sub Procedure Saat Tombol Hapus Diklik
Double klik tombol hapus dan masukkan kode berikut :
- Private Sub cmdHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHapus.Click
- If Me.txtKodeBarang.Text = “” Then Exit Sub
- Dim rep As Integer = MessageBox.Show(“Data tersebut akan dihapus?”, “Konfirmasi”, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
- If rep = MsgBoxResult.No Then Exit Sub
- Dim deleteBrg = (From barang In db.barangs _
- Where barang.KDBarang = Me.txtKodeBarang.Text).ToList()(0)
- db.barangs.DeleteOnSubmit(deleteBrg)
- Try
- db.SubmitChanges()
- Catch ex As Exception
- MessageBox.Show(“Error :” & ex.Message)
- End Try
- resData()
- dgKlik()
- End Sub
7. Sub Procedure Saat Tombol Keluar Diklik
Double klik tombol keluar dan masukkan kode berikut :
- Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click
- Me.Close()
- End Sub
Terakhir, tambahkan kode berikut pada bagian frmBarang_Load :
- Private Sub frmBarang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- resData()
- isiCombo()
- buka(False)
- Me.cmdSimpan.Enabled = False
- t = False
- End Sub
Yups, dengan semua tombol telah memiliki kode masing-masing, coba jalankan program dengan mengklik tanda
play atau menekan tombol F5 pada keyboard.
Pada bagian selanjutnya kita akan langsung mencoba membuat form untuk
menampilkan data laporan dari data barang yang barusan dibuat..
Selamat bereksperimen semoga sukses…
Kode Selengkapnya :
- Public Class frmBarang
- Private db As New DataClasses1DataContext
- Private edit As Boolean
- Private Sub frmBarang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- resData()
- isiCombo()
- buka(False)
- Me.cmdSimpan.Enabled = False
- edit = False
- End Sub
- Private Sub buka(ByVal t As Boolean)
-
- Dim daftarTextBox = From myControl In Me.Controls _
- Where TypeOf myControl Is TextBox _
- Select myControl
-
- For Each myTeksBoks As TextBox In daftarTextBox
- If t Then
- myTeksBoks.Enabled = True
- Me.cmbKodeKategori.Enabled = True
- Else
- myTeksBoks.Enabled = False
- Me.cmbKodeKategori.Enabled = False
- End If
- Next
- End Sub
- Private Sub isiCombo()
- Me.cmbKodeKategori.ResetText()
-
- Dim cmbKate = From kategori In db.kategoris _
- Select kategori.NmKategori
-
- For Each kate As String In cmbKate
- Me.cmbKodeKategori.Items.Add(kate)
- Next
- End Sub
- Private Sub resData()
- Dim qBarang = From barang In db.barangs _
- Join kategori In db.kategoris _
- On kategori.KDKategori Equals barang.KDKategori _
- Select Kode = barang.KDBarang, Nama = barang.NmBarang, _
- Kategori = kategori.NmKategori, Jumlah = barang.Jumlah, Harga = barang.HargaJual
- Me.DataGridView1.DataSource = qBarang
- End Sub
- Private Sub resTeks()
- isiCombo()
- Dim daftarTextBox = From myControl In Me.Controls _
- Where TypeOf myControl Is TextBox _
- Select myControl
- For Each myTeksBoks As TextBox In daftarTextBox
- myTeksBoks.Text = “”
- Next
- End Sub
- Private Sub dgKlik()
- Try
- Me.txtKodeBarang.Text = Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value
- Me.txtNamaBarang.Text = Me.DataGridView1.Item(1, Me.DataGridView1.CurrentRow.Index).Value
- Me.txtJumlah.Text = Me.DataGridView1.Item(3, Me.DataGridView1.CurrentRow.Index).Value
- Me.txtHarga.Text = Me.DataGridView1.Item(4, Me.DataGridView1.CurrentRow.Index).Value
-
- Dim dkate = From kategori In db.kategoris _
- Where kategori.NmKategori Is Me.DataGridView1.Item(2, Me.DataGridView1.CurrentRow.Index).Value _
- Select kategori.NmKategori
- Me.cmbKodeKategori.Text = dkate.Single
- Catch ex As Exception
- MessageBox.Show(“Error: ” & ex.Message)
- End Try
- End Sub
- Private Function katPerNama(ByVal n As String) As Integer
- Dim kat As Integer = 0
- Dim kate = From kategori In db.kategoris _
- Where kategori.NmKategori Is n _
- Select kategori.KDKategori
- kat = kate.Single
- Return kat
- End Function
- Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
- dgKlik()
- End Sub
- Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
-
- Me.cmdBaru.Text = “Batal”
- Me.cmdEdit.Enabled = False
- Me.cmdHapus.Enabled = False
- Me.cmdSimpan.Enabled = True
- edit = True
- buka(True)
- Me.txtKodeBarang.Enabled = False
- Me.txtNamaBarang.Focus()
- End Sub
- Private Sub cmdBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBaru.Click
-
-
- If Me.cmdBaru.Text = “Baru” Then
- Me.cmdBaru.Text = “Batal”
- Me.resTeks()
- buka(True)
- Me.DataGridView1.Enabled = False
- Me.cmdSimpan.Enabled = True
- Me.cmdEdit.Enabled = False
- Me.cmdHapus.Enabled = False
- Me.cmdKeluar.Enabled = False
- Me.txtKodeBarang.Focus()
- Else
- Me.cmdBaru.Text = “Baru”
- buka(False)
- Me.DataGridView1.Enabled = True
- edit = False
- Me.cmdSimpan.Enabled = False
- Me.cmdEdit.Enabled = True
- Me.cmdHapus.Enabled = True
- Me.cmdKeluar.Enabled = True
- dgKlik()
- End If
- End Sub
- Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSimpan.Click
-
- If edit Then
- Dim updatebrg = (From barang In db.barangs _
- Where barang.KDBarang = Me.txtKodeBarang.Text).ToList()(0)
- With updatebrg
- .NmBarang = Me.txtNamaBarang.Text
- .KDKategori = katPerNama(Me.cmbKodeKategori.Text)
- .Jumlah = Me.txtJumlah.Text
- .HargaJual = Me.txtHarga.Text
- End With
- Else
- Dim brg As New barang With { _
- .KDBarang = Me.txtKodeBarang.Text, _
- .NmBarang = Me.txtNamaBarang.Text, _
- .KDKategori = katPerNama(Me.cmbKodeKategori.Text), _
- .Jumlah = Me.txtJumlah.Text, _
- .HargaJual = Me.txtHarga.Text}
- End If
- Try
- db.SubmitChanges()
- Catch ex As Exception
- MessageBox.Show(“Error :” & ex.Message)
- End Try
- resData()
- dgKlik()
- buka(False)
- Me.cmdSimpan.Enabled = False
- Me.cmdBaru.Text = “Baru”
- Me.cmdEdit.Enabled = True
- Me.cmdKeluar.Enabled = True
- edit = False
- Me.cmdHapus.Enabled = True
- Me.DataGridView1.Enabled = True
- End Sub
- Private Sub cmdHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHapus.Click
-
- If Me.txtKodeBarang.Text = “” Then Exit Sub
-
- Dim rep As Integer = MessageBox.Show(“Data tersebut akan dihapus?”, “Konfirmasi”, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
- If rep = MsgBoxResult.No Then Exit Sub
- Dim deleteBrg = (From barang In db.barangs _
- Where barang.KDBarang = Me.txtKodeBarang.Text).ToList()(0)
- db.barangs.DeleteOnSubmit(deleteBrg)
- Try
- db.SubmitChanges()
- Catch ex As Exception
- MessageBox.Show(“Error :” & ex.Message)
- End Try
- resData()
- dgKlik()
- End Sub
- Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click
- Me.Close()
-
- End Sub
- End Class
Sorry kalau masih ada yang keliru atau kesalahan dalam proses penulisan di atas..mohon koreksinya juga.. heheh..e