Public Class usrCtrSales3
Private bsSales As New BindingSource 'เก็บข้อมูลรายละเอียดการขาย (Item1, Item2, Item3, ...)
Private Sub usrCtrProduct_Load(sender As Object, e As EventArgs) Handles Me.Load
dgvSaleItems.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
dgvSaleItems.Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
dgvSaleItems.Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
dgvSaleItems.Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
Call MakeDataTableDEMO_Data()
End Sub
Private Sub MakeDataTableDEMO_Data()
Dim dtSales As New DataTable
dtSales.Columns.Add("Part_NO", GetType(String))
dtSales.Columns.Add("Part_Desc", GetType(String))
dtSales.Columns.Add("xQTY", GetType(Double))
dtSales.Columns.Add("xPrice", GetType(Double))
dtSales.Columns.Add("xTotal", GetType(Double))
dtSales.Columns.Add("xDiscount", GetType(Double))
dtSales.Rows.Add(New Object() {"P-001", "สินค้าตัวอย่าง1", 2, 50, 100, 0})
dtSales.Rows.Add(New Object() {"P-002", "สินค้าตัวอย่าง2", 5, 30, 150, 0})
dtSales.Rows.Add(New Object() {"P-003", "สินค้าตัวอย่าง3", 4, 6, 24, 0})
dtSales.AcceptChanges() 'Commit Change.
bsSales.DataSource = dtSales
SetDGVInfo(bsSales)
End Sub
Private Sub SetDGVInfo(ByVal bs As BindingSource)
dgvSaleItems.AutoGenerateColumns = False
dgvSaleItems.Columns(0).DataPropertyName = "Part_NO"
dgvSaleItems.Columns(1).DataPropertyName = "Part_Desc"
dgvSaleItems.Columns(2).DataPropertyName = "xQTY"
dgvSaleItems.Columns(3).DataPropertyName = "xPrice"
dgvSaleItems.Columns(4).DataPropertyName = "xTotal"
dgvSaleItems.Columns(5).DataPropertyName = "xDiscount"
dgvSaleItems.DataSource = bsSales
End Sub
'กลับไปก่อนหน้า
Private Sub BacktoPanelHead()
pnlDetail1.Visible = False
pnlDetail2.Visible = False
pnlHead.Visible = True
pnlDetail2.Dock = DockStyle.Bottom
pnlDetail1.Dock = DockStyle.Bottom
pnlHead.Dock = DockStyle.Fill
End Sub
'คลิ๊กปุ่ม เพิ่มข้อมูล
Private Sub tsbAction_Click(sender As Object, e As EventArgs) Handles tsbAdd.Click
Actions("A")
End Sub
'คลิ๊กปุ่มแก้ไขข้อมูล
Private Sub tsbEdit_Click(sender As Object, e As EventArgs) Handles tsbEdit.Click
Actions("E")
End Sub
'RowState (A/E/D)
Private Sub Actions(ByVal mode As String)
pnlDetail1.Visible = True
pnlDetail2.Visible = False
pnlHead.Visible = False
pnlDetail1.Dock = DockStyle.Fill
pnlDetail2.Dock = DockStyle.None
pnlHead.Dock = DockStyle.None
lblActions.Text = If(mode = "A", "เพิ่มข้อมูล", "แก้ไขข้อมูล")
If mode = "E" Then
'Binding to ALL TextBox
For i As Integer = 0 To Me.Controls.Count - 1
'Binding To TextBox Control
'Binding to ComboBox Control
'and so on.
Next
Else 'Edit Mode
'Reset All TextBox/ComboBox/etc...
mduControlManager.GetAllControl(pnlDetail1, GetType(WL_Controls.usrCtrlTextBox)).ToList().ForEach(Sub(x) DirectCast(x, WL_Controls.usrCtrlTextBox).ResetValue())
mduControlManager.GetAllControl(pnlDetail1, GetType(WL_Controls.usrCtrComboBox)).ToList().ForEach(Sub(x) DirectCast(x, WL_Controls.usrCtrComboBox).ResetValue())
End If
End Sub
'กลับไปก่อนหน้า
Private Sub tsbBack_Click(sender As Object, e As EventArgs) Handles tsbBack.Click
'ถ้าไม่มีการใช้ BindingSource (Me.dgvSaleItems.Rows.Clear())
BacktoPanelHead()
End Sub
Private Sub dgvSaleItems_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvSaleItems.EditingControlShowing
'TODO
'More...
End Sub
Private Sub dgvSaleItems_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvSaleItems.CellValidating
'Clear All error message.
dgvSaleItems.Rows(e.RowIndex).ErrorText = String.Empty
If dgvSaleItems.Rows(e.RowIndex).IsNewRow Then 'มีอยู่ 1 แถวและไม่มีอะไรเลย
Exit Sub 'Return 'Exist Sub
End If
If dgvSaleItems.CurrentCell.IsInEditMode Then
If e.ColumnIndex = 0 Then
dgvSaleItems.Rows(e.RowIndex).Cells(1).Value = Guid.NewGuid()
dgvSaleItems.Rows(e.RowIndex).Cells(2).Value = 1.0
dgvSaleItems.Rows(e.RowIndex).Cells(3).Value = 200.0
dgvSaleItems.Rows(e.RowIndex).Cells(4).Value = 200.0
dgvSaleItems.Rows(e.RowIndex).Cells(5).Value = 0.0
End If
End If
If e.ColumnIndex > 1 Then 'Column 2 is readonly (array zero base.)
Dim newDouble As Double
If Not Double.TryParse(If(e.FormattedValue = String.Empty, 0, e.FormattedValue.ToString()), newDouble) OrElse newDouble < 0 Then
e.Cancel = True
Me.dgvSaleItems.Rows(e.RowIndex).ErrorText = "ต้องป้อนเป็นตัวเลขเท่านั้น"
End If
End If
End Sub
'คำนวณ รวมเงิน = จำนวน x ราคา/หน่วย
Private Sub dgvSaleItems_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSaleItems.CellValidated
If Not dgvSaleItems.CurrentRow.IsNewRow Then
If e.ColumnIndex > 1 Then
dgvSaleItems.Rows(e.RowIndex).Cells(4).Value = dgvSaleItems.Rows(e.RowIndex).Cells(2).Value * dgvSaleItems.Rows(e.RowIndex).Cells(3).Value
End If
End If
End Sub
Private Sub dgvSaleItems_CellMouseClickORDbClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvSaleItems.CellMouseClick, dgvSaleItems.CellMouseDoubleClick
Try
If String.IsNullOrEmpty(dgvSaleItems.Rows(e.RowIndex).Cells(0).Value) Then
dgvSaleItems.CurrentCell = dgvSaleItems(0, e.RowIndex)
Return
End If
Dim col As Short = dgvSaleItems.CurrentCell.ColumnIndex
If dgvSaleItems.Columns(col).Visible = False OrElse dgvSaleItems.Columns(col).ReadOnly = True OrElse dgvSaleItems.Columns(col).CellType.ToString = "System.Windows.Forms.DataGridViewImageCell" Then
For iSub As Short = col To dgvSaleItems.ColumnCount - 1
col = (col + 1) Mod dgvSaleItems.ColumnCount
If dgvSaleItems.Columns(col).Visible = True AndAlso dgvSaleItems.Columns(col).ReadOnly = False AndAlso _
dgvSaleItems.Columns(col).CellType.ToString <> "System.Windows.Forms.DataGridViewImageCell" Then
Exit For
End If
Next
End If
dgvSaleItems.CurrentCell = dgvSaleItems.CurrentRow.Cells(col)
Catch ex As Exception
'log hear.
End Try
End Sub
'
'ถ้าอยู่ใน EditMode มันจะไม่เข้ามาที่อีเวนต์นี้
Private Sub dgvSaleItems_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvSaleItems.KeyDown
If dgvSaleItems.CurrentCell.ColumnIndex = 0 AndAlso e.KeyCode = Keys.Right Then
e.Handled = True
dgvSaleItems.CurrentCell = dgvSaleItems(2, dgvSaleItems.CurrentCell.RowIndex)
End If
If dgvSaleItems.CurrentCell.ColumnIndex = 2 AndAlso e.KeyCode = Keys.Left Then
e.Handled = True
dgvSaleItems.CurrentCell = dgvSaleItems(0, dgvSaleItems.CurrentCell.RowIndex)
End If
If dgvSaleItems.CurrentCell.ColumnIndex > 1 AndAlso (e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Enter) Then
e.Handled = True
If dgvSaleItems.CurrentCell.RowIndex + 2 >= dgvSaleItems.Rows.Count Then
dgvSaleItems.CurrentCell = dgvSaleItems(0, dgvSaleItems.Rows.Count - 1)
Else
If dgvSaleItems.CurrentCell.ColumnIndex = 5 Then
dgvSaleItems.CurrentCell = dgvSaleItems.Rows(dgvSaleItems.CurrentCell.RowIndex).Cells(0)
Else
dgvSaleItems.CurrentCell = dgvSaleItems.Rows(dgvSaleItems.CurrentCell.RowIndex).Cells(dgvSaleItems.CurrentCell.ColumnIndex + 1)
End If
End If
End If
End Sub
'แสดงลำดับที่ของแถว 1, 2, 3, ...
'
Private Sub dgvSaleItems_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgvSaleItems.RowPostPaint
Try
Dim length As String = dgvSaleItems.RowCount.ToString.Length
Dim row As String = (e.RowIndex + 1).ToString.PadLeft(length, " ")
Dim size As SizeF = e.Graphics.MeasureString("ก", Me.Font)
Using b As SolidBrush = New SolidBrush(dgvSaleItems.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString(row, dgvSaleItems.DefaultCellStyle.Font, b, e.RowBounds.Location.X + ((dgvSaleItems.RowHeadersWidth - size.Width) / 2) + 1, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
End Using
Catch
'logger error.
End Try
End Sub
'ทดสอบการลบข้อมูลโดยการ Double Click ณ. หัวแถวนั้นฯ (ทดสอบ)
Private Sub dgvSaleItems_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvSaleItems.RowHeaderMouseDoubleClick
If bsSales.Position <> -1 Then
DirectCast(bsSales.DataSource, DataTable).Rows(bsSales.Position).Delete() 'By Ref
End If
End Sub
'ตรวจสอบ RowState (FoxPro ทำได้ตั้งแต่ 20 ปีที่แล้ว ทั้ง Field State และ Row State)
Private Sub btnGetRowState_Click(sender As Object, e As EventArgs) Handles btnGetRowState.Click
Dim dt = DirectCast(bsSales.DataSource, DataTable)
If dt IsNot Nothing Then
Dim rowStateDeleted = dt.GetChanges(DataRowState.Deleted)
Dim rowStateModifyed = dt.GetChanges(DataRowState.Modified)
Dim rowStateAdded = dt.GetChanges(DataRowState.Added)
'F9 Break
If "ขนขึ้น" <> "หอยไม่ขึ้น" Then
End If
End If
End Sub
End Class
Imports System.Windows.Forms
Public Class myTextBoxColumn : Inherits System.Windows.Forms.DataGridViewColumn
Private mAllowDecimal As Boolean
Private mAllowMinus As Boolean
Private mAllowDateSep As Boolean
Private mAllowInputType As String
Private mMaxInputlength As Integer
Private mCelltemplate As myTextBoxCell
#Region "Properties"
Public Property AllowInputType As String
Get
Return mAllowInputType
End Get
Set(value As String)
mAllowInputType = value
mCelltemplate.AllowInputType = value
End Set
End Property
Public Property AllowDecimal() As Boolean
Get
Return mAllowDecimal
End Get
Set(ByVal value As Boolean)
mAllowDecimal = value
mCelltemplate.AllowDecimal = value
End Set
End Property
Public Property AllowMinus() As Boolean
Get
Return mAllowMinus
End Get
Set(ByVal value As Boolean)
mAllowMinus = value
mCelltemplate.AllowMinus = value
End Set
End Property
Public Property AllowDateSep() As Boolean
Get
Return mAllowDateSep
End Get
Set(ByVal value As Boolean)
mAllowDateSep = value
mCelltemplate.AllowDateSep = value
End Set
End Property
Public Property MaxInputLength() As Integer
Get
Return mMaxInputlength
End Get
Set(ByVal value As Integer)
mMaxInputlength = value
mCelltemplate.MaxInputLength = value
End Set
End Property
#End Region
Public Sub New()
MyBase.New(New myTextBoxCell)
mCelltemplate = MyBase.CellTemplate
End Sub
Public Sub New(ByVal dec As Boolean, ByVal min As Boolean, ByVal sep As Boolean, ByVal len As Integer, ByVal inputType As String)
MyBase.New(New myTextBoxCell(dec, min, sep, len, inputType))
mCelltemplate = MyBase.CellTemplate
Me.mAllowDecimal = dec
Me.mAllowMinus = min
Me.mAllowDateSep = sep
Me.mMaxInputlength = len
Me.mAllowInputType = inputType
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
MyBase.CellTemplate = value
End Set
End Property
''' <summary>
''' Fixed Bug Design time. lose value
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overrides Function Clone() As Object
Dim myClone As myTextBoxColumn = CType(MyBase.Clone, myTextBoxColumn)
myClone.AllowDecimal = AllowDecimal
myClone.AllowMinus = AllowMinus
myClone.AllowDateSep = AllowDateSep
myClone.MaxInputLength = MaxInputLength
myClone.AllowInputType = AllowInputType
Return myClone
End Function
End Class
Code (VB.NET)
Imports System.Windows.Forms
Public Class myTextBoxEditingControl : Inherits DataGridViewTextBoxEditingControl
Private Const mDec As Char = "."
Private Const mMinus As Char = "-"c
Private Const mDateSep As Char = "/"c
Private mAllowDecimal As Boolean
Private mAllowMinus As Boolean
Private mAllowDateSep As Boolean
Private mMaxInputlength As Integer
Private mAllowInputType As String
#Region "Properties"
Public Property AllowInputType() As String
Get
Return mAllowInputType
End Get
Set(ByVal value As String)
mAllowInputType = value
End Set
End Property
Public Property AllowDecimal() As Boolean
Get
Return mAllowDecimal
End Get
Set(ByVal value As Boolean)
mAllowDecimal = value
End Set
End Property
Public Property AllowMinus() As Boolean
Get
Return mAllowMinus
End Get
Set(ByVal value As Boolean)
mAllowMinus = value
End Set
End Property
Public Property AllowDateSep() As Boolean
Get
Return mAllowDateSep
End Get
Set(ByVal value As Boolean)
mAllowDateSep = value
End Set
End Property
Public Property MaxInputLength() As Integer
Get
Return mMaxInputlength
End Get
Set(ByVal value As Integer)
mMaxInputlength = value
End Set
End Property
#End Region
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal dec As Boolean, ByVal min As Boolean, ByVal sla As Boolean, ByVal len As Integer, ByVal inputType As String)
MyBase.New()
mAllowDecimal = dec
mAllowMinus = min
mAllowDateSep = sla
mMaxInputlength = len
mAllowInputType = inputType
End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Select Case AllowInputType.ToLower()
Case "integer"
If e.KeyChar < "0" Or e.KeyChar > "9" Then
If AscW(e.KeyChar) = Keys.Back Then 'Backspace
e.Handled = False
Else
e.Handled = True
End If
Else
MyBase.OnKeyPress(e)
End If
Case "double"
If e.KeyChar < "0" Or e.KeyChar > "9" Then
If AscW(e.KeyChar) = Keys.Back Then
e.Handled = False
Else
If e.KeyChar = "." And MyBase.Text.Contains(".") = False Then
e.Handled = False
ElseIf e.KeyChar = "-" And MyBase.Text.Contains("-") = False Then
e.Handled = False
Else
e.Handled = True
End If
End If
Else
MyBase.OnKeyPress(e)
End If
End Select
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If Me.Text.Length >= mMaxInputlength Then
e.Handled = True
Else
MyBase.OnKeyDown(e)
End If
End Sub
End Class
Code (VB.NET)
Imports System.Windows.Forms
Public Class myTextBoxEditingControl : Inherits DataGridViewTextBoxEditingControl
Private Const mDec As Char = "."
Private Const mMinus As Char = "-"c
Private Const mDateSep As Char = "/"c
Private mAllowDecimal As Boolean
Private mAllowMinus As Boolean
Private mAllowDateSep As Boolean
Private mMaxInputlength As Integer
Private mAllowInputType As String
#Region "Properties"
Public Property AllowInputType() As String
Get
Return mAllowInputType
End Get
Set(ByVal value As String)
mAllowInputType = value
End Set
End Property
Public Property AllowDecimal() As Boolean
Get
Return mAllowDecimal
End Get
Set(ByVal value As Boolean)
mAllowDecimal = value
End Set
End Property
Public Property AllowMinus() As Boolean
Get
Return mAllowMinus
End Get
Set(ByVal value As Boolean)
mAllowMinus = value
End Set
End Property
Public Property AllowDateSep() As Boolean
Get
Return mAllowDateSep
End Get
Set(ByVal value As Boolean)
mAllowDateSep = value
End Set
End Property
Public Property MaxInputLength() As Integer
Get
Return mMaxInputlength
End Get
Set(ByVal value As Integer)
mMaxInputlength = value
End Set
End Property
#End Region
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal dec As Boolean, ByVal min As Boolean, ByVal sla As Boolean, ByVal len As Integer, ByVal inputType As String)
MyBase.New()
mAllowDecimal = dec
mAllowMinus = min
mAllowDateSep = sla
mMaxInputlength = len
mAllowInputType = inputType
End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
'If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
' If e.KeyChar <> Convert.ToChar(System.Windows.Forms.Keys.Back) Then
' If mAllowDecimal AndAlso e.KeyChar = mDec _
' OrElse mAllowMinus AndAlso e.KeyChar = mMinus _
' OrElse mAllowDateSep AndAlso e.KeyChar = mDateSep Then
' MyBase.OnKeyPress(e)
' Else
' e.Handled = True
' End If
' Else
' MyBase.OnKeyPress(e)
' End If
'Else
' MyBase.OnKeyPress(e)
'End If
Select Case AllowInputType.ToLower()
Case "integer"
If e.KeyChar < "0" Or e.KeyChar > "9" Then
If AscW(e.KeyChar) = Keys.Back Then 'Backspace
e.Handled = False
Else
e.Handled = True
End If
Else
MyBase.OnKeyPress(e)
End If
Case "double"
If e.KeyChar < "0" Or e.KeyChar > "9" Then
If AscW(e.KeyChar) = Keys.Back Then
e.Handled = False
Else
If e.KeyChar = "." And MyBase.Text.Contains(".") = False Then
e.Handled = False
ElseIf e.KeyChar = "-" And MyBase.Text.Contains("-") = False Then
e.Handled = False
Else
e.Handled = True
End If
End If
Else
MyBase.OnKeyPress(e)
End If
End Select
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If Me.Text.Length >= mMaxInputlength Then
e.Handled = True
Else
MyBase.OnKeyDown(e)
End If
End Sub
End Class