Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.Data
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Model
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns


Public Class WinGroupFooterViewController
    Inherits ViewController(Of ListView)
    Public Sub New()
        MyBase.New()

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

    End Sub
    Private Sub View_InfoSynchronized(ByVal sender As Object, ByVal e As EventArgs)
        Dim modelListView As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)
        If modelListView IsNot Nothing AndAlso modelListView.IsGroupFooterVisible Then
            Dim gridListEditor As GridListEditor = TryCast(View.Editor, GridListEditor)
            If gridListEditor IsNot Nothing Then
                Dim gridView As GridView = gridListEditor.GridView
                If gridView IsNot Nothing Then
                    For i As Integer = 0 To gridView.GroupSummary.Count - 1
                        Dim modelColumn As IModelColumnExtender = TryCast(CType(View.Model.Columns, IModelList(Of IModelColumn))(gridView.GroupSummary(i).FieldName), IModelColumnExtender)
                        If modelColumn IsNot Nothing Then
                            modelColumn.GroupFooterSummaryType = gridView.GroupSummary(i).SummaryType
                        End If
                    Next i
                End If
            End If
        End If
    End Sub
    Protected Overloads Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        Dim displayFormat As String
        Dim modelListView As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)
        If modelListView IsNot Nothing AndAlso modelListView.IsGroupFooterVisible Then
            Dim gridListEditor As GridListEditor = TryCast(View.Editor, GridListEditor)
            If gridListEditor IsNot Nothing Then
                Dim gridView As GridView = gridListEditor.GridView
                gridView.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways
                For Each modelColumn As IModelColumn In View.Model.Columns
                    Dim modelColumnExtender As IModelColumnExtender = TryCast(modelColumn, IModelColumnExtender)
                    If modelColumnExtender IsNot Nothing AndAlso modelColumnExtender.GroupFooterSummaryType <> SummaryItemType.None Then
                        Dim gridColumn As GridColumn = gridView.Columns(modelColumn.ModelMember.MemberInfo.BindingName)
                        If modelColumn.DisplayFormat.ToString <> "" Then
                            If Mid(modelColumn.DisplayFormat, 1, 1) = "{" Then
                                displayFormat = modelColumn.DisplayFormat
                                gridView.GroupSummary.Add(modelColumnExtender.GroupFooterSummaryType, modelColumn.Id, gridColumn, displayFormat)
                            Else
                                displayFormat = String.Format("{{0:{0}}}", modelColumn.DisplayFormat)
                                gridView.GroupSummary.Add(modelColumnExtender.GroupFooterSummaryType, modelColumn.Id, gridColumn, displayFormat)
                            End If
                            
                        Else
                            gridView.GroupSummary.Add(modelColumnExtender.GroupFooterSummaryType, modelColumn.Id, gridColumn)
                        End If

                    End If
                Next modelColumn
            End If
        End If
    End Sub
    Protected Overloads Overrides Sub OnActivated()
        MyBase.OnActivated()
        AddHandler View.ModelSaved, AddressOf View_InfoSynchronized
    End Sub
    Protected Overloads Overrides Sub OnDeactivated()
        RemoveHandler View.ModelSaved, AddressOf View_InfoSynchronized
        MyBase.OnDeactivated()
    End Sub
End Class



