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.Web.Editors.ASPx
Imports DevExpress.Web
Imports DevExpress.ExpressApp.Model

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

    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If TryCast(View, ListView) IsNot Nothing Then
            'AddHandler View.ControlsCreated, AddressOf View_Controlcreated
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        'If TryCast(View, ListView) IsNot Nothing Then
        '    Dim gridListEditor As ASPxGridListEditor = TryCast(View.Editor, ASPxGridListEditor)
        '    If gridListEditor IsNot Nothing Then
        '        Dim _Grid As ASPxGridView = TryCast(gridListEditor.Grid, ASPxGridView)
        '        If _Grid IsNot Nothing And GLOBAL_ListViewReset = False Then
        '            GetColumnWithfromGrid(_Grid)
        '            View.SaveModel()
        '        End If
        '    End If
        'End If
    End Sub
    Private Sub View_Controlcreated(sender As Object, e As EventArgs)
        Dim gridListEditor As ASPxGridListEditor = TryCast(View.Editor, ASPxGridListEditor)
        If gridListEditor IsNot Nothing Then
            Dim _Grid As ASPxGridView = TryCast(gridListEditor.Grid, ASPxGridView)
            If _Grid IsNot Nothing Then
                AddHandler _Grid.HtmlDataCellPrepared, AddressOf Grid_HtmlDataCellPrepared

                _Grid.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.Control
                '_Grid.Settings.UseFixedTableLayout = True
                _Grid.Settings.HorizontalScrollBarMode = DevExpress.Web.ScrollBarMode.Auto
                _Grid.Settings.UseFixedTableLayout = False
                '_Grid.Settings.
                SetColumnWithfromModel(_Grid)

                _Grid.SettingsPager.Position = System.Web.UI.WebControls.PagerPosition.TopAndBottom
                Dim _IModelListViewExtender As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)
                If _IModelListViewExtender IsNot Nothing Then
                    _Grid.Settings.GroupFormat = _IModelListViewExtender.GridViewGroupFormat
                End If

            End If
        End If
    End Sub
    Private Sub SetColumnWithfromModel(_Grid As ASPxGridView)
        For Each column As WebColumnBase In _Grid.Columns
            If (TypeOf column Is GridViewDataColumn) Then
                column.Width = CType(column, GridViewDataColumn).Width
            End If
        Next
    End Sub
    Private Sub GetColumnWithfromGrid(_Grid As ASPxGridView)
        For Each column As WebColumnBase In _Grid.Columns
            If (TypeOf column Is GridViewDataColumn) Then
                CType(column, GridViewDataColumn).Width = column.Width.Value
            End If
        Next
    End Sub
    Private Sub Grid_BeforeColumnSortingGrouping(sender As Object, e As ASPxGridViewBeforeColumnGroupingSortingEventArgs)
        Dim _IModelListViewExtender As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)
        If _IModelListViewExtender IsNot Nothing Then
            sender.Settings.GroupFormat = _IModelListViewExtender.GridViewGroupFormat
        End If
    End Sub


    Private Sub Grid_HtmlDataCellPrepared(sender As Object, e As ASPxGridViewTableDataCellEventArgs)
        Dim modelColumn As IModelColumnExtender = Nothing

        If View IsNot Nothing Then
            If e.DataColumn.FieldName <> "" Then
                If e.DataColumn.FieldName.Contains(".") Then
                    Dim NewName As String() = e.DataColumn.FieldName.Split(New Char() {"."c}, StringSplitOptions.RemoveEmptyEntries)
                    modelColumn = TryCast(CType(View.Model.Columns, IModelList(Of IModelColumn))(NewName(NewName.Length - 1)), IModelColumnExtender)
                Else
                    modelColumn = TryCast(CType(View.Model.Columns, IModelList(Of IModelColumn))(e.DataColumn.FieldName), IModelColumnExtender)
                End If
                If modelColumn IsNot Nothing Then
                    If modelColumn.HideValueIfZero = True Then
                        If e.CellValue = 0 Then e.Cell.Text = ""
                    End If
                End If

            End If
        End If
    End Sub

End Class
