Private Sub dataGridViewData_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs)
Try
If e.RowIndex > -1 AndAlso e.ColumnIndex > -1 Then
Select Case dataGridViewData.Columns(e.ColumnIndex).Name
Case "Qty", "Price"
Dim Price As [Double] = 0
Dim Qty As [Double] = 0
Dim Amount As [Double] = 0
If dataGridViewData.CurrentRow.Cells("Price").Value IsNot Nothing Then
[Double].TryParse(dataGridViewData.CurrentRow.Cells("Price").Value.ToString(), Price)
End If
[Double].TryParse(dataGridViewData.CurrentRow.Cells("Qty").Value.ToString(), Qty)
Amount = Qty * Price
dataGridViewData.CurrentRow.Cells("Amount").Value = Util.ToDecimal(Amount.ToString("#,###.00"))
CalCulate()
Exit Select
Case Else
Exit Select
'CalCulate();
End Select
End If
Catch ex As Exception
Util.MsgError(Util.GetMSGError(ex), "เกิดข้อผิดพลาด")
End Try
End Sub
Private dt As New DataTable
Private dr As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.AutoGenerateColumns = False
CreateDTProduct()
End Sub
Private Sub CreateDTProduct()
dt.Columns.Add("ProductId", GetType(String))
dt.Columns.Add("ProductName", GetType(String))
dt.Columns.Add("Qty", GetType(Integer))
dt.Columns.Add("Price", GetType(Double))
dt.Columns.Add("TotalPrice", GetType(Double))
End Sub
Private Function ValidationData() As Boolean
If (txtProductId.Text = "") Then
MsgBox("ตรวจสอบ รหัสสินค้า")
Return False
End If
If (txtProductName.Text = "") Then
MsgBox("ตรวจสอบ ชื่อสินค้า")
Return False
End If
If (txtQty.Text = "") Then
MsgBox("ตรวจสอบ จำนวนสินค้า")
Return False
End If
If (cboPrice.Text = "") Then
MsgBox("ตรวจสอบ ราคาสินค้า")
Return False
End If
If (Microsoft.VisualBasic.Information.IsNumeric(txtQty.Text) = False) Then
MsgBox("ตรวจสอบ จำนวนสินค้า")
Return False
End If
If (Microsoft.VisualBasic.Information.IsNumeric(cboPrice.Text) = False) Then
MsgBox("ตรวจสอบ ราคาสินค้า")
Return False
End If
Return True
End Function
Private Sub TotalSumPrice()
If (dt.Rows.Count <> 0) Then
Dim _ttSumPrice As Double = 0
For index As Integer = 0 To dt.Rows.Count - 1
_ttSumPrice = _ttSumPrice + Double.Parse(dt.Rows(index).Item("TotalPrice").ToString())
Next
txtTotalSumPrice.Text = _ttSumPrice.ToString("#,##0.00")
End If
End Sub
Private Sub btnAddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddProduct.Click
If (ValidationData()) Then
If DataGridView1.Rows.Count = 0 Then
dr = dt.NewRow()
dr("ProductId") = txtProductId.Text
dr("ProductName") = txtProductName.Text
dr("Qty") = txtQty.Text
dr("Price") = cboPrice.Text
dr("TotalPrice") = txtTotalPrice.Text
dt.Rows.Add(dr)
dr = Nothing
Else
Dim _chkAddPrd As Boolean = False
For index As Integer = 0 To dt.Rows.Count - 1
'ตรวจสอบ รหัสสินค้าที่เหมือนกัน
If (txtProductId.Text = dt.Rows(index).Item("ProductId").ToString()) Then
Dim _qtyGrd As Integer = Integer.Parse(dt.Rows(index).Item("Qty").ToString()) 'จำนวน
Dim _ttPrice As Integer = Integer.Parse(dt.Rows(index).Item("Price").ToString()) 'ราคา
dt.Rows(index).Item("Qty") = _qtyGrd + Integer.Parse(txtQty.Text) 'บวกจำนวนเพิ่ม
dt.Rows(index).Item("TotalPrice") = _ttPrice * Integer.Parse(dt.Rows(index).Item("Qty").ToString()) 'ผล ราคารวมใหม่
_chkAddPrd = True
Exit For
End If
Next
If _chkAddPrd = False Then ' ไม่มีสินค้าเหมือนกัน ให้เพิ่มใหม่
dr = dt.NewRow()
dr("ProductId") = txtProductId.Text
dr("ProductName") = txtProductName.Text
dr("Qty") = txtQty.Text
dr("Price") = cboPrice.Text
dr("TotalPrice") = txtTotalPrice.Text
dt.Rows.Add(dr)
dr = Nothing
End If
End If
TotalSumPrice()
DataGridView1.DataSource = dt
End If
End Sub