Hasil dari from transaksi penjualan adalah seperti gambar dibawah ini :
Berikut langkahnya :
Silahakn buka VB .NET anda
Bautlah sebuah project VB .NET
Jika sudah muncul Form1, silahkan design form1 seperti gambar dibawah ini :
DataGridView yang diganti namanya menjadi : dgvData
Button Tambah : btnAdd
Button Hapus : btnRemove
Label Total
dan TexBox : txtSumTotal
JIka sudah selesai membuat Form seperti gambar diatas,
Silahkan Masukan Koding pada Form1 dibawah ini :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Me.Text = "Contoh Form Transaksi Penjualan - www.javanetmedia.com"
txtSumTotal.Text = "0.00"
SetupGridView()
Dim row As String() = New String() {1, "BRG0001", "Nama Barang 1", 1, 0.0, 0.0}
' Add First Row
dgvData.Rows.Add(row)
' Focus
dgvData.Focus()
dgvData.CurrentCell = dgvData.Rows(dgvData.RowCount - 1).Cells(3)
' / Summary Cell Index = 5
CalSumTotal()
End Sub
' / --------------------------------------------------------------------------------
Private Sub SetupGridView()
With dgvData
.RowHeadersVisible = False
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeRows = False
.MultiSelect = False
.ReadOnly = False
' Columns Specified
dgvData.ColumnCount = 6
dgvData.Columns(0).Name = "No"
'dgvData.Columns(0).Visible = False ' Don't show Primary Key
dgvData.Columns(1).Name = "Kode_Barang"
dgvData.Columns(2).Name = "Nama_barang"
dgvData.Columns(3).Name = "Jumlah"
dgvData.Columns(3).ValueType = GetType(Integer)
dgvData.Columns(4).Name = "Harga"
dgvData.Columns(4).ValueType = GetType(Double)
dgvData.Columns(5).Name = "Total"
dgvData.Columns(5).ValueType = GetType(Double)
' Hidden column index 0 ... It's Primary Key for DataBase
.Columns("No").Visible = True ' False
.Columns("Kode_Barang").HeaderText = "Kode_Barang"
.Columns("Nama_Barang").HeaderText = "Nama_Barang"
.Columns("Jumlah").HeaderText = "Jumlah"
.Columns("Harga").HeaderText = "Harga"
' Total Column
With .Columns("Total")
.HeaderText = "Total"
.ReadOnly = True
.DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow
.DefaultCellStyle.ForeColor = System.Drawing.Color.Red
.DefaultCellStyle.Font = New Font(dgvData.Font, FontStyle.Bold)
End With
' Adjust Column width
.Columns(0).Width = 35
.Columns(1).Width = dgvData.Width \ 5
.Columns(2).Width = dgvData.Width \ 5
.Columns(3).Width = dgvData.Width \ 5 - 20
.Columns(4).Width = dgvData.Width \ 5 - 20
.Columns(5).Width = dgvData.Width \ 5 ' + 2
' Header Own Style
With .ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(dgvData.Font, FontStyle.Bold)
End With
' Alignment only columns 3 to 5
For i As Byte = 3 To 5
' Header Alignment
.Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
' Cell Alignment
.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Next
End With
End Sub
' / --------------------------------------------------------------------------------
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
With dgvData
.Width = Me.Width - 22
End With
txtSumTotal.Left = Me.Width - (txtSumTotal.Width + 20)
Label1.Location = New Point(txtSumTotal.Left - Me.Label1.Width, txtSumTotal.Top + 3)
End Sub
' / --------------------------------------------------------------------------------
' / Add new row
Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim Position As Integer = dgvData.Rows.Count - 1
Dim LastRow = (From Items In dgvData.Rows.Cast(Of DataGridViewRow)() Select Items).ElementAt(Position)
Dim PK As Long = LastRow.Cells(0).Value + 1
Dim row As String() = New String() {PK, "BRG000" & PK, "Nama Barang " & PK, 1, 0.0, 0.0}
dgvData.Rows.Add(row)
' Focus
dgvData.CurrentCell = dgvData.Rows(dgvData.RowCount - 1).Cells(3)
dgvData.Focus()
' /
CalSumTotal()
End Sub
' / --------------------------------------------------------------------------------
' / Remove selected row
Private Sub btnDelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
If dgvData.RowCount = 1 Then Exit Sub
dgvData.Rows.Remove(dgvData.CurrentRow)
dgvData.Refresh()
' /
CalSumTotal()
End Sub
' / --------------------------------------------------------------------------------
' / Calcualte sum of Total (Column Index = 5)
Private Sub CalSumTotal()
txtSumTotal.Text = "0.00"
For i As Byte = 0 To dgvData.RowCount - 1
' CDbl = Convert to Double
txtSumTotal.Text = CDbl(txtSumTotal.Text) + CDbl(dgvData.Rows(i).Cells(5).Value)
Next
txtSumTotal.Text = Format(CDbl(txtSumTotal.Text), "#,##0.00")
End Sub
' / --------------------------------------------------------------------------------
' / Before Edit Data
Private Sub dgvData_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvData.CellBeginEdit
Dim Quantity As Integer = dgvData.Rows(e.RowIndex).Cells(3).Value
Dim UnitPrice As Double = dgvData.Rows(e.RowIndex).Cells(4).Value
dgvData.Rows(e.RowIndex).Cells(5).Value = (Quantity * UnitPrice).ToString("#,##0.00")
' /
CalSumTotal()
End Sub
' / --------------------------------------------------------------------------------
' / After you press Enter
Private Sub dgvData_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
Select Case e.ColumnIndex
Case 3, 4 ' Column Index = 3 (Quantity), Column Index = 4 (UnitPrice)
' Quantity
' If Null Value
If IsDBNull(dgvData.Rows(e.RowIndex).Cells(3).Value) Then dgvData.Rows(e.RowIndex).Cells(3).Value = 0
Dim Quantity As Integer = dgvData.Rows(e.RowIndex).Cells(3).Value
' Keep original value in temporary. (Tag Properties)
dgvData.Tag = Quantity
If Quantity <= 0 Then
dgvData.Rows(e.RowIndex).Cells(3).Value = dgvData.Tag
Quantity = dgvData.Tag
End If
' UnitPrice
' If Null Value
If IsDBNull(dgvData.Rows(e.RowIndex).Cells(4).Value) Then dgvData.Rows(e.RowIndex).Cells(4).Value = 0.0
Dim UnitPrice As Double = dgvData.Rows(e.RowIndex).Cells(4).Value
dgvData.Tag = UnitPrice
If UnitPrice < 0 Then
dgvData.Rows(e.RowIndex).Cells(4).Value = dgvData.Tag
UnitPrice = dgvData.Tag
End If
' Quantity x UnitPrice
dgvData.Rows(e.RowIndex).Cells(5).Value = (Quantity * UnitPrice).ToString("#,##0.00")
CalSumTotal()
End Select
End Sub
' / --------------------------------------------------------------------------------
Private Sub dgvData_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvData.EditingControlShowing
Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Name
' / Can use both ColumeIndex or Field Name
Case "Quantity", "UnitPrice"
RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
End Select
End Sub
' / --------------------------------------------------------------------------------
Private Sub ValidKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim tb As TextBox = sender
Select Case dgvData.CurrentCell.ColumnIndex
Case 3 ' Quantity is Integer
Select Case e.KeyChar
Case "0" To "9" ' digits 0 - 9 allowed
Case ChrW(Keys.Back) ' backspace allowed for deleting (Delete key automatically overrides)
Case ChrW(Keys.Delete) ' Delete key
Case Else ' everything else ....
' True = CPU cancel the KeyPress event
e.Handled = True ' and it's just like you never pressed a key at all
End Select
Case 4 ' UnitPrice is Double
Select Case e.KeyChar
Case "0" To "9"
' Allowed "."
Case "."
' can present "." only one
If InStr(tb.Text, ".") Then e.Handled = True
Case ChrW(Keys.Back)
Case ChrW(Keys.Delete)
Case Else
e.Handled = True
End Select
End Select
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
Application.Exit()
End Sub
End Class
Jika sudah silahkan anda Save dan Jalankan Project VB .NET andaDemikian Source Code Contoh From Transaksi Penjualan VB .NET, semoga bermanfaat untuk anda
Selain sebagai media informasi pemrograman, kami juga berbagi artikel terkait tips trik Android.
No comments:
Post a Comment