Combobox yang mempunyai event di DataGridView
Oke langsung saja, implementasinya langsung ke contoh project saja yah, sekarang siapkan database access 2003 new database dan kasih nama latihan.mdb. dan buat beberapa table dengan ketentuan seperti berikut :
Relasinya jangan lupa ditentukan yah.
Buat tampilan di VB.NET kira-kira seperti ini dengan nama form nya yaitu frm_Transaksi:
Dan setiap objek dikasih nama sesuai dengan fungsinya yah..
Setelah itu new item lagi dan buat class baru dengan nama Cls_Induk.vb
Coding ini di dalam class tersebut :
1: Imports System.IO2: Public Class Cls_Induk
3: Private myConnString As String
4: Private conn As OleDbConnection
5: 'constructor
6: Public Sub New() 7: MyBase.New() 8: setConnString() 9: conn = New OleDbConnection 10: conn.ConnectionString = myConnString 11: End Sub 12: Private Sub setConnString() 13: myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\latihan.mdb" 14: End Sub 15: 'pembebasan memori 16: Private Sub dispose() 17: conn = Nothing 18: myConnString = Nothing 19: GC.SuppressFinalize(Me)20: End Sub
21: 'destructor
22: Protected Overrides Sub Finalize() 23: dispose() 24: End Sub 25: Public ReadOnly Property connString() As String 26: Get 27: Return myConnString 28: End Get 29: End Property 30: Public Function openDB() As Boolean 31: If conn.State = ConnectionState.Open Then 32: conn.Close() 33: End If 34: Try 35: conn.Open() 36: Return True 37: Catch ex As Exception 38: Return False 39: End Try 40: End Function 41: Public Sub closeDB() 42: If conn.State = ConnectionState.Open Then conn.Close() 43: End Sub 44: Protected Function getDataTable(ByVal strSQL As String) As DataTable 45: Dim myAdapter As OleDbDataAdapter 46: Dim myData As New DataTable 47: Dim myCommand As OleDbCommand 48: openDB() 49: myCommand = New OleDbCommand(strSQL, conn) 50: myAdapter = New OleDbDataAdapter(myCommand) 51: myAdapter.Fill(myData) 52: closeDB() 53: Return myData 54: 'pembebasan memori 55: myAdapter = Nothing 56: myData = Nothing 57: myCommand = Nothing58: End Function
59: Protected Function getDataSet(ByVal strSQL As String) As DataSet
60: Dim myAdapter As OleDbDataAdapter
61: Dim myDataSet As New DataSet
62: Dim myCommand As OleDbCommand
63: openDB()64: myCommand = New OleDbCommand(strSQL, conn)
65: myAdapter = New OleDbDataAdapter(myCommand)
66: myAdapter.Fill(myDataSet) 67: closeDB()68: Return myDataSet
69: 'pembebasan memori
70: myAdapter = Nothing 71: myDataSet = Nothing 72: myCommand = Nothing 73: End Function 74: 'untuk errors75: Protected Function insert(ByVal strSQL As String) As Boolean
76: Return execute(strSQL, "Insert Data")
77: End Function
78: Protected Function delete(ByVal strSQL As String) As Boolean
79: Return execute(strSQL, "Delete Data")
80: End Function
81: Protected Function update(ByVal strSQL As String) As Boolean
82: Return execute(strSQL, "Update Data")
83: End Function
84: Protected Function search(ByVal strSQL As String) As Boolean
85: Dim dt As New DataTable
86: dt = getDataTable(strSQL)87: If dt.Rows.Count > 0 Then
88: Return True
89: Else
90: Return False
91: End If
92: End Function
93: Private Function execute(ByVal strSQL As String, ByVal tipePerintah As String) As Boolean
94: Try95: Dim myCommand As OleDbCommand
96: myCommand = New OleDbCommand(strSQL)
97: openDB()98: myCommand.Connection = conn
99: myCommand.ExecuteNonQuery() 100: closeDB() 101: 'pembebasan memori 102: myCommand = Nothing103: Return True
104: Catch ex As Exception
105: MessageBox.Show(ex.Message, tipePerintah, MessageBoxButtons.OK)106: Return False
107: End Try
108: End Function
109: End Class
1: Public Class Cls_Transaksi
2: Inherits Cls_Induk3: Private strInit As String
4: Private KdTransaksi As String
5: Private Tanggal As String
6: Private Pelanggan As String
7: Private KdPetugas As Short
8: Private KdProduct As Short
9: Private Banyaknya As Integer
10: Private SubTotal As Double
11: Private Total As Double
12: Public Property setStrInit() As String
13: Get
14: Return strInit
15: End Get
16: Set(ByVal value As String)
17: strInit = value
18: End Set
19: End Property
20: Public Property setKdTransaksi() As String
21: Get
22: Return KdTransaksi
23: End Get
24: Set(ByVal value As String)
25: KdTransaksi = value
26: End Set
27: End Property
28: Public Property setTanggal() As String
29: Get
30: Return Tanggal
31: End Get
32: Set(ByVal value As String)
33: Tanggal = value
34: End Set
35: End Property
36: Public Property setPelanggan() As String
37: Get
38: Return Pelanggan
39: End Get
40: Set(ByVal value As String)
41: Pelanggan = value
42: End Set
43: End Property
44: Public Property setKdPetugas() As Short
45: Get
46: Return KdPetugas
47: End Get
48: Set(ByVal value As Short)
49: KdPetugas = value
50: End Set
51: End Property
52: Public Property setKdProduct() As Short
53: Get
54: Return KdProduct
55: End Get
56: Set(ByVal value As Short)
57: KdProduct = value
58: End Set
59: End Property
60: Public Property setBanyaknya() As Integer
61: Get
62: Return Banyaknya
63: End Get
64: Set(ByVal value As Integer)
65: Banyaknya = value
66: End Set
67: End Property
68: Public Property setSubTotal() As Double
69: Get
70: Return SubTotal
71: End Get
72: Set(ByVal value As Double)
73: SubTotal = value
74: End Set
75: End Property
76: Public Property setTotal() As Double
77: Get
78: Return Total
79: End Get
80: Set(ByVal value As Double)
81: Total = value
82: End Set
83: End Property
84: Public Sub New()
85: MyBase.New()
86: strInit = "SELECT ht.kdTransaksi,ht.Tanggal,p.NmPelanggan,tk.NmToko,pr.NmProduk,pr.Harga,dt.Banyaknya,dt.SubTotal,pt.NmPetugas from tbl_HTransaksi ht,tbl_DTransaksi dt,tbl_Produk pr,tbl_Petugas pt,tbl_Toko tk,tbl_Pelanggan p where ht.KdPelanggan=p.KdPelanggan and dt.KdProduk=pr.KdProduk and ht.KdPetugas=pt.KdPetugas and ht.KdToko=tk.KdToko and ht.KdTransaksi = dt.KdTransaksi"
87: End Sub
88: Private Sub dispose() 89: strInit = Nothing 90: GC.SuppressFinalize(Me)91: End Sub
92: Protected Overrides Sub Finalize() 93: MyBase.Finalize() 94: dispose()95: End Sub
96: Public Function getAll() As DataTable
97: Dim strSQL As String = strInit
98: Return MyBase.getDataTable(strSQL)
99: End Function
100: Public Function getProduk() As DataTable
101: Dim strSQL As String
102: strSQL = "select NmProduk from tbl_Produk order by KdProduk"
103: Return MyBase.getDataTable(strSQL)
104: End Function
105: Public Function getHargaProduk(ByVal nama As String) As DataTable
106: Dim strSQL As String
107: strSQL = "select KdProduk,Harga from tbl_Produk where NmProduk='" & nama & "'"
108: Return MyBase.getDataTable(strSQL)
109: End Function
110: Public Function getPetugas() As DataTable
111: Dim strSQL As String
112: strSQL = "select NmPetugas from tbl_Petugas order by KdPetugas"
113: Return MyBase.getDataTable(strSQL)
114: End Function
115: Public Function getKode(ByVal kode As String, ByVal nama As String, ByVal parNm As String, ByVal tbl As String) As DataTable
116: Dim strSQL As String
117: strSQL = "select " & kode & " from " & tbl & " where " & nama & " like '" & parNm & "'"
118: Return MyBase.getDataTable(strSQL)
119: End Function
120: Public Function getNoRek() As DataTable
121: Dim strSQL As String
122: strSQL = "select count(*) from tbl_HTransaksi"
123: Return MyBase.getDataTable(strSQL)
124: End Function
125: Public Overloads Function insertHTransaksi(ByVal obj As Cls_Transaksi) As Boolean
126: Dim strSql As String
127: strSql = "insert into tbl_HTransaksi values('" & obj.setKdTransaksi _
128: & "','" & obj.setTanggal _
129: & "','" & obj.setPelanggan _
130: & "','" & obj.setKdPetugas _
131: & "','" & obj.Total _
132: & "')"133: Return MyBase.insert(strSql)
134: End Function
135: Public Overloads Function insertDTransaksi(ByVal obj As Cls_Transaksi) As Boolean
136: Dim strSql As String
137: strSql = "insert into tbl_DTransaksi(KdTransaksi,KdProduk,Banyaknya,Subtotal) values('" & obj.setKdTransaksi _
138: & "','" & obj.setKdProduct _
139: & "','" & obj.setBanyaknya _
140: & "','" & obj.setSubTotal _
141: & "')"142: Return MyBase.insert(strSql)
143: End Function
144: End Class
1: Public Class frm_Transaksi
2: Dim listCol As New DataGridViewComboBoxColumn
3: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
4: DataGridView1.Rows.Clear()
5: bersih()6: End Sub
7: Sub bersih() 8: txtJumlah.Text = "" 9: txtBayar.Text = "" 10: txtKembali.Text = "" 11: lblTampil.Text = "" 12: txtTuan.Text = ""13: End Sub
14: Sub Petugas()15: Dim dt As New DataTable
16: dt = TR.getPetugas() 17: cmbPetugas.DataSource = dt 18: cmbPetugas.DisplayMember = "NmPetugas"19: End Sub
20: Sub Norek()21: Dim dt2 As New DataTable
22: dt2 = TR.getNoRek23: txtNota.Text = "TRASK000" & dt2.Rows(0).Item(0) + 1
24: End Sub
25: Sub Produk()26: 'Produk
27: DataGridView1.Columns.Remove("NamaBarang") 28: Dim dt As New DataTable 29: dt = TR.getProduk 30: listCol.DisplayIndex = 1 31: listCol.HeaderText = "NAMA BARANG"32: 'Masukkan nama field pada table yang akan di buat listcolom
33: listCol.DataPropertyName = "NamaBarang"34: 'Mengisi list dari Nama Produk table
35: listCol.DataSource = dt 36: listCol.DisplayMember = "NmProduk" 37: listCol.ValueMember = "NmProduk"38: 'Tambahkan column
39: DataGridView1.Columns.Add(listCol)
40: listCol.Width = 28341: End Sub
42: Private Sub frm_Transaksi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
43: Petugas() 44: Norek() 45: Produk()46: 'Membuat Readolny
47: DataGridView1.Columns(2).ReadOnly = True 48: DataGridView1.Columns(1).ReadOnly = True 49: End Sub 50: Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 51: Dim cb As ComboBox = TryCast(e.Control, ComboBox) 52: If cb IsNot Nothing Then 53: If DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value <> "" Then 54: RemoveHandler cb.SelectedIndexChanged, AddressOf DataGridView1_SelectedIndexChanged 55: AddHandler cb.SelectedIndexChanged, AddressOf DataGridView1_SelectedIndexChanged 56: Else 57: MessageBox.Show("Isi Data Banyaknya Terlebih Dahulu") 58: End If 59: End If 60: End Sub 61: Private Sub DataGridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 62: Try63: 'Ketikkan sintaks untuk menampilkan data yang di inginkan
64: Dim comboBox1 As ComboBox = CType(sender, ComboBox)
65: Dim dt As New DataTable
66: dt = TR.getHargaProduk(comboBox1.Text)67: DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = dt.Rows(0).Item("Harga")
68: Hitung() 69: Total()70: txtBayar.ReadOnly = False
71: btnSimpan.Enabled = True
72: Catch 73: MessageBox.Show("Pilih produknya yah...!! ", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)74: Exit Try
75: End Try
76: End Sub
77: Sub Hitung()78: DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value = Format(CInt(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value) * CInt(DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value), "Currency")
79: End Sub
80: Sub Total()81: Dim Total As Double = 0
82: For i As Integer = 0 To DataGridView1.Rows.Count - 2
83: Total = Total + CDbl(DataGridView1.Item(2, i).Value)
84: Next i
85: txtJumlah.Text = Total 86: lblTampil.Text = Total87: End Sub
88: Dim TR As New Cls_Transaksi
89: Sub InsertSet()90: 'insert ke Header Transaksi
91: Dim dt, dt1, dt2, dt3 As New DataTable92: 'mencari kode masing-masing dari table master
93: dt1 = TR.getKode("KdPetugas", "NmPetugas", cmbPetugas.Text, "tbl_Petugas")94: TR.setKdPetugas = CInt(dt1.Rows(0).Item("KdPetugas"))
95: TR.setPelanggan = txtTuan.Text 96: TR.setKdTransaksi = txtNota.Text 97: TR.setTanggal = DateTimePicker1.Text 98: TR.setTotal = txtJumlah.Text 99: TR.insertHTransaksi(TR)100: 'insert ke Detail Transaksi sesuai dengan data yang di inputkan ke DATAGRIDVIEW oleh user
101: For i As Integer = 0 To DataGridView1.Rows.Count - 2
102: dt3 = TR.getKode("KdProduk", "NmProduk", DataGridView1.Item(3, i).Value, "tbl_Produk")
103: TR.setKdProduct = CInt(dt3.Rows(0).Item("KdProduk"))
104: TR.setBanyaknya = CInt(DataGridView1.Item(0, i).Value)
105: TR.setSubTotal = CDbl(DataGridView1.Item(2, i).Value)
106: TR.insertDTransaksi(TR)107: Next i
108: End Sub
109: Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
110: If txtTuan.Text = "" Then
111: MessageBox.Show("Tentukan atau isi terlebih dahulu nama pelanggan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)112: Else
113: If CDbl(txtBayar.Text) > CDbl(txtJumlah.Text) Then
114: btnTRBaru.Enabled = True
115: InsertSet()116: MessageBox.Show("Data Tersimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
117: btnSimpan.Enabled = False
118: Else
119: MessageBox.Show("Anda uangnya kurang", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information) 120: txtBayar.Text = "" 121: txtBayar.Focus()122: End If
123: End If
124: End Sub
125: Private Sub btnTRBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTRBaru.Click
126: DataGridView1.Rows.Clear()
127: bersih()128: btnSimpan.Enabled = True
129: btnTRBaru.Enabled = False
130: Norek()131: End Sub
132: Private Sub txtBayar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBayar.KeyPress
133: If e.KeyChar = Chr(13) Then
134: txtKembali.Text = CDbl(txtBayar.Text) - CDbl(txtJumlah.Text) 135: lblTampil.Text = CDbl(txtBayar.Text) - CDbl(txtJumlah.Text)136: End If
137: End Sub
138: Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click
139: If MessageBox.Show("Apakah anda yakin akan keluar dari program ini ..??", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
140: Me.Close()
141: End If
142: End Sub
143: End Class
SOURCE : http://netvbnet.blogspot.com/
Label: Programming



0 Komentar:
Posting Komentar
Berlangganan Posting Komentar [Atom]
<< Beranda