Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Data.Filtering

Public Class ContractRowsFreeVC
    Inherits DevExpress.ExpressApp.ViewController(Of ListView)

    Private _GridListEditor As GridListEditor
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Contracts_ContractRows_ListView" Then
            If _GridListEditor IsNot Nothing Then
                Dim _GridView As XafGridView = TryCast(_GridListEditor.GridView, XafGridView)
                AddHandler _GridView.ShowingEditor, AddressOf _GridView_ShowingEditor
            End If
        End If
        If View.Id = "StorageInHeader_StorageInRows_ListView" Then
            If _GridListEditor IsNot Nothing Then
                RemoveHandler _GridListEditor.GridView.CellValueChanged, AddressOf Contracts_ContractRows_ListView_CellValueChanged
            End If
        End If
    End Sub

    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "Contracts_ContractRows_ListView" Or _
            View.Id = "ContractRows_ListView_Invoice_Not_MileStone" Then
            Dim _ListView As ListView = CType(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
            _GridListEditor = TryCast(View.Editor, GridListEditor)
            If _GridListEditor IsNot Nothing Then
                AddHandler _GridListEditor.GridView.CellValueChanged, AddressOf Contracts_ContractRows_ListView_CellValueChanged
            End If
            If _GridListEditor IsNot Nothing Then
                '_GridListEditor.AllowEdit = True
                Dim _GridView As XafGridView = TryCast(_GridListEditor.GridView, XafGridView)
                If _GridView IsNot Nothing Then
                    AddHandler _GridView.ShowingEditor, AddressOf _GridView_ShowingEditor
                End If
                'For i As Integer = 0 To _GridListEditor.Columns.Count - 1
                '    If _GridListEditor.GridView.Columns(i).FieldName <> "NettoPriceHUF" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "NettoPriceDEV" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "ShortName" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "Description" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "InvoicePeriod" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "Controlling!" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "PaymentOption!" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "VAT!" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "CurrencyRate" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "QTT" And _
                '        _GridListEditor.GridView.Columns(i).FieldName <> "DateExecution" Then

                '        _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
                '        _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
                '        _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
                '    End If
                'Next i
            End If

        End If
    End Sub
    Sub _GridView_ShowingEditor(ender As Object, e As System.ComponentModel.CancelEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        If _ContractRows.RegistryRowsFinancialControlling IsNot Nothing Or _
            _ContractRows.CertificateofContractRows IsNot Nothing Then
            e.Cancel = True
        End If
    End Sub

    Private Sub Contracts_ContractRows_ListView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Try
                Dim parentView As View = nestedFrame.View
                If parentView IsNot Nothing Then
                    parentView.ObjectSpace.CommitChanges()
                End If

                If Frame.GetController(Of RefreshController).RefreshAction.Active Then
                    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
                End If
            Catch exp As Exception
                MsgBox(exp.Message)
            End Try
        Else
            Frame.View.ObjectSpace.CommitChanges()

            If Frame.GetController(Of RefreshController).RefreshAction.Active Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        End If
    End Sub
End Class
