Private Sub dgvDryingOrganicLongan_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvDryingOrganicLonganBake.CellValidating
If e.ColumnIndex = -1 Then Exit Sub
If e.RowIndex = -1 Then Exit Sub
'เช็คตัวเลขColumn8
If dgvDryingOrganicLonganBake.Columns(e.ColumnIndex).Name = "dgvLmaiDryWeight" Or dgvDryingOrganicLonganBake.Columns(e.ColumnIndex).Name = "dgvWeightLoss" Or _
dgvDryingOrganicLonganBake.Columns(e.ColumnIndex).Name = "Column8" Then
If CStr(e.FormattedValue) <> "" Then
If IsNumeric(e.FormattedValue) = False Then
e.Cancel = True
MessageBox.Show("ไม่สามารถใส่ตัวอักษรได้ ใส่ได้เฉพาะตัวเลขเท่านั้น!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
End If
End If
'#########################################################################################
'#########################################################################################
If dgvDryingOrganicLonganBake.Columns(e.ColumnIndex).Name = "Column2" Then
If CStr(e.FormattedValue) <> "" Then
Dim dtCountLot As New DataTable()
Dim CountLot As Integer = 1
Dim Sql As String = "SELECT LotNo FROM DryingOrganicLonganBake WHERE LotNo=@LotNo"
Com = New SqlCommand()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
For i = 0 To 8
With Com
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = Sql
.Connection = Conn
.Parameters.Add("@LotNo", SqlDbType.NVarChar).Value = "D" & CountLot & CDate(e.FormattedValue).Day().ToString("00") & CDate(e.FormattedValue).Month().ToString("00") & CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(0).Value).Remove(0, 5).Remove(2, 6)
dr = .ExecuteReader()
If dr.HasRows Then
dtCountLot.Load(dr)
CountLot = CountLot + 1
Else
Exit For
End If
End With
dr.Close()
Next
dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(2).Value = "D" & CountLot & CDate(e.FormattedValue).Day().ToString("00") & CDate(e.FormattedValue).Month().ToString("00") & CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(0).Value).Remove(0, 5).Remove(2, 6)
End If
End If
'###################################################################################################################
If dgvDryingOrganicLonganBake.Columns(e.ColumnIndex).Name = "dgvWeightLoss" Then
If CStr(e.FormattedValue) <> "" Then
If CDbl(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(4).Value) < CDbl(e.FormattedValue) Then
e.Cancel = True
MessageBox.Show("คุณกรอกจำนวนน้ำหนักที่สูญเสียมากกว่าจำนวนน้ำหนักรับเข้า !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value) = "" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value = 0
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value) = "" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value = 0
dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value = CDbl(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(4).Value) - CDbl(e.FormattedValue)
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value) = "0" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value = ""
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value) = "0" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value = ""
End If
ElseIf dgvDryingOrganicLonganBake.Columns(e.ColumnIndex).Name = "dgvLmaiDryWeight" Then
If CStr(e.FormattedValue) <> "" Then
If CDbl(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(4).Value) < CDbl(e.FormattedValue) Then
e.Cancel = True
MessageBox.Show("คุณกรอกจำนวนน้ำหนักลำไยแห้งมากกว่าจำนวนน้ำหนักรับเข้า !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value) = "" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value = 0
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value) = "" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value = 0
dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value = CDbl(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(4).Value) - CDbl(e.FormattedValue)
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value) = "0" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(5).Value = ""
If CStr(dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value) = "0" Then dgvDryingOrganicLonganBake.Rows(e.RowIndex).Cells(6).Value = ""
End If
End If
End Sub