Imports DevExpress.XtraVerticalGrid.Rows

Public Class frmQuickInput
    Dim Isinsert As Boolean = False

    Private Sub frmQuickInput_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim sql As String = "SELECT * FROM VWTEMP_PMO_FORM" & CURRENT_FORM_ID
        Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
        If DT Is Nothing = False Then
            'DT.Columns.Remove("Record-ID")
            DT.Columns.Item("Record-ID").ReadOnly = True
            DT.Columns.Remove("Form-ID")
            DT.Columns.Remove("AddedWho")
            DT.Columns.Remove("AddedWhen")
            DT.Columns.Remove("ChangedWho")
            DT.Columns.Remove("ChangedWhen")
            BindingSourceQuickInput.DataSource = Nothing
            Me.BindingSourceQuickInput.DataSource = DT
            vGridQuickInput.DataSource = BindingSourceQuickInput
        Else
            BindingSourceQuickInput.DataSource = Nothing
        End If

        tsStatus.Visible = False
    End Sub

    Private Sub BindingSource1_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles BindingSourceQuickInput.AddingNew
        Isinsert = True
    End Sub

    Private Sub tsbtnsave_Click(sender As Object, e As EventArgs) Handles tsbtnsave.Click
        Dim RECORD_ID As Integer
        Dim CONTROL_ID As Integer

        tsStatus.Visible = False

        If Isinsert = True Then
            ' Create the record first
            Dim Sql As String = "INSERT INTO TBPMO_RECORD(ADDED_WHO, FORM_ID) VALUES ('" & Environment.UserName & "', " & CURRENT_FORM_ID & ")"
            If ClassDatabase.Execute_non_Query(Sql) = True Then
                ' Get last RecordID 
                Sql = "SELECT (MAX(GUID)) FROM TBPMO_RECORD where ADDED_WHO = '" & Environment.UserName & "'"
                RECORD_ID = ClassDatabase.Execute_Scalar(Sql)

                ' Make sure all values are saved
                vGridQuickInput.CloseEditor()
                For Each row As BaseRow In vGridQuickInput.Rows
                    Dim val As Object = vGridQuickInput.GetCellValue(row, vGridQuickInput.FocusedRecord)
                    Dim fieldName As String = row.Properties.FieldName

                    CONTROL_ID = ClassFunctionCommands.GETCONTROL_ID_FOR_COL_NAME(CURRENT_FORM_ID, fieldName)
                    If CONTROL_ID <> 0 Then
                        If ClassFunctionCommands.InsertValue(CONTROL_ID, RECORD_ID, val) Then
                            tsStatus.Visible = True
                            tsStatus.Text = "Datensatz erfolgreich eingefügt - " & Date.Now
                        Else
                            tsStatus.Visible = True
                            tsStatus.Text = "Fehler beim Einfügen des Datensatzes - " & Date.Now
                        End If
                    End If
                Next row

                Isinsert = False
            End If

        Else
            ' Make sure all values are saved
            vGridQuickInput.CloseEditor()
            For Each row As BaseRow In vGridQuickInput.Rows
                Dim val As Object = vGridQuickInput.GetCellValue(row, vGridQuickInput.FocusedRecord)
                Dim fieldName As String = row.Properties.FieldName

                If fieldName = "Record-ID" Then
                    RECORD_ID = val
                End If

                CONTROL_ID = ClassFunctionCommands.GETCONTROL_ID_FOR_COL_NAME(CURRENT_FORM_ID, fieldName)
                If CONTROL_ID <> 0 Then
                    If ClassFunctionCommands.UpdateValue(CONTROL_ID, RECORD_ID, val) = True Then
                        tsStatus.Visible = True
                        tsStatus.Text = "Änderungen erfolgreich gespeichert - " & Date.Now
                    Else
                        tsStatus.Visible = True
                        tsStatus.Text = "Fehler beim Speichern des Datensatzes - " & Date.Now
                    End If
                End If
            Next row
        End If


    End Sub


End Class