Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Controls.Primitives
Imports System.Windows.Input
Imports System.Windows.Media
Imports EgtUILib
Imports EgtWPFLib5

Public Class EgtDataGrid

   Public Event ResetSort()

   ' vecchio indice da salvare per il Reorder delle colonne
   Public OldIndex As Integer = 0
   ' indice header su cui è aperto il ContextMenu per gestione check/uncheck LockTable
   Private HeaderDispIndex As Integer = 0
   ' somma delle larghezze delle colonne di EgtDataGrid
   Private columnsWidth As Double = 0

   ' larghezza minima dell'ultima colonna per attivare/disattivare ScrollBar orizzontale
   Private Const LASTCOLUMN_MINWIDTH As Double = 40

   Sub New()
      InitializeComponent()
   End Sub

   Public Shadows Property BindingColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return CType(MyBase.GetValue(BindingColumnsProperty), ObservableCollection(Of EgtDataGridColumn))
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         MyBase.SetValue(BindingColumnsProperty, value)
      End Set
   End Property

   Public Shared ReadOnly BindingColumnsProperty As DependencyProperty = DependencyProperty.Register("BindingColumns", GetType(ObservableCollection(Of EgtDataGridColumn)), GetType(EgtDataGrid), New FrameworkPropertyMetadata(New ObservableCollection(Of EgtDataGridColumn), New PropertyChangedCallback(AddressOf OnDataGridColumnsPropertyChanged)) With {.BindsTwoWayByDefault = True})

   Private Shared Sub OnDataGridColumnsPropertyChanged(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
      Dim context = TryCast(source, EgtDataGrid)
      Dim oldItems = TryCast(e.OldValue, ObservableCollection(Of EgtDataGridColumn))

      If oldItems IsNot Nothing Then

         For index = 0 To oldItems.Count - 1
            Dim Column As DataGridColumn = context.FindResource(oldItems(index))
            context.Columns.Remove(Column)
         Next

         RemoveHandler oldItems.CollectionChanged, AddressOf context.collectionChanged
      End If

      Dim newItems = TryCast(e.NewValue, ObservableCollection(Of EgtDataGridColumn))

      If newItems IsNot Nothing Then

         For index = 0 To newItems.Count - 1
            Try
               Dim col As DataGridColumn = context.FindResource(newItems(index).Name)
               newItems(index).InitColumn(col)
               context.Columns.Add(col)
            Catch ex As Exception
               MessageBox.Show(String.Format(EgtMsg(61883), newItems(index).Name, newItems(index).ParentDataGridName), EgtMsg(15002), MessageBoxButton.OK, MessageBoxImage.Warning)
            End Try
         Next

         AddHandler newItems.CollectionChanged, AddressOf context.collectionChanged
      End If
   End Sub

   Private Sub collectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
      Select Case e.Action
         Case NotifyCollectionChangedAction.Add

            If e.NewItems IsNot Nothing Then

               For Each one As EgtDataGridColumn In e.NewItems
                  Try
                     Dim col As DataGridColumn = CurrDataGrid.FindResource(one.Name)
                     one.InitColumn(col)
                     CurrDataGrid.Columns.Insert(e.NewStartingIndex, col)
                     If col.DisplayIndex <> e.NewStartingIndex Then
                        CurrDataGrid.Columns(e.NewStartingIndex).DisplayIndex = e.NewStartingIndex
                     End If
                  Catch ex As Exception
                     MessageBox.Show(String.Format(EgtMsg(61883), one.Name, one.ParentDataGridName), EgtMsg(15002), MessageBoxButton.OK, MessageBoxImage.Warning)
                  End Try
               Next
            End If

         Case NotifyCollectionChangedAction.Remove

            If e.OldItems IsNot Nothing Then

               For Each one As EgtDataGridColumn In e.OldItems
                  Dim col As DataGridColumn = CurrDataGrid.FindResource(one.Name)
                  CurrDataGrid.Columns.Remove(col)
               Next
            End If

         Case NotifyCollectionChangedAction.Move
            'Dim col As DataGridColumn = CurrDataGrid.FindResource(BindingColumns(e.OldStartingIndex))
            'col.DisplayIndex = e.NewStartingIndex
            CurrDataGrid.Columns.Move(e.OldStartingIndex, e.NewStartingIndex)
            CurrDataGrid.Columns(e.NewStartingIndex).DisplayIndex = e.NewStartingIndex
            'Dim x = CurrDataGrid.Columns(e.OldStartingIndex).DisplayIndex
         Case NotifyCollectionChangedAction.Reset
            CurrDataGrid.Columns.Clear()

            If e.NewItems IsNot Nothing Then

               For Each one As DataGridColumn In e.NewItems
                  Dim col As DataGridColumn = CurrDataGrid.FindResource(one)
                  CurrDataGrid.Columns.Add(col)
               Next
            End If
         Case NotifyCollectionChangedAction.Replace
            Dim col As DataGridColumn = CurrDataGrid.FindResource(e.NewItems(0))
            CurrDataGrid.Columns(e.NewStartingIndex) = col
      End Select
   End Sub

   Private Sub Cell_SizedChanged(sender As Object, e As SizeChangedEventArgs)
      ' ad ogni ridimensionamento della cella del ColumnHeader salvo le nuove dimensioni nell'ini
      If Not IsNothing(e.OriginalSource.Column) AndAlso Not IsNothing(BindingColumns(e.OriginalSource.Column.DisplayIndex)) Then
         ' se la colonna corrente è Star ed è preceduta da una colonna Pixel non scrivo le modifiche
         If Not (e.OriginalSource.Column.Width.IsStar) Then
            BindingColumns(e.OriginalSource.Column.DisplayIndex).SaveDataGridColumn()
         End If
      End If
   End Sub

   Private Sub DataGrid_SizedChanged(sender As Object, e As SizeChangedEventArgs) Handles CurrDataGrid.SizeChanged
      ' confronto la larghezza effettiva della EgtDataGrid con la somma delle larghezze delle colonne
      UpdateColumnsWidth()
      If BindingColumns.Count > 0 Then
         If CurrDataGrid.ActualWidth - LASTCOLUMN_MINWIDTH > columnsWidth Then
            ' nascondo la ScrollBar orizzontale e setto lo UnitType dell'ultima colonna visibile a Star
            ScrollViewer.SetHorizontalScrollBarVisibility(CurrDataGrid, ScrollBarVisibility.Hidden)
            BindingColumns.Last(Function(x) x.Visible = True).Width = New DataGridLength(1, DataGridLengthUnitType.Star)
         Else
            ' mostro la ScrollBar orizzontale e se UnitType delle colonne è Star lo setto a Pixel
            ScrollViewer.SetHorizontalScrollBarVisibility(CurrDataGrid, ScrollBarVisibility.Visible)
            For Each bc In BindingColumns
               If bc.Width.IsStar Then
                  bc.Width = New DataGridLength(bc.ActualWidth, DataGridLengthUnitType.Pixel)
               End If
            Next
         End If
         ' scrivo nell'INI la Width dell'ultima colonna visibile
         BindingColumns.Last(Function(x) x.Visible = True).SaveDataGridColumn()
      End If
   End Sub

   Public Sub UpdateColumnsWidth()
      ' ricalcolo la somma delle larghezze delle colonne visibili
      columnsWidth = 0
      For Each col In BindingColumns
         If col.ColumnVisibility = Visibility.Visible Then
            columnsWidth += col.ActualWidth
         End If
      Next
      ' alla somma calcolata tolgo la larghezza dell'ultima colonna visibile e aggiungo LASTCOLUMN_MINWIDTH
      ' (in questo modo si attiva/disattiva la ScrollBar orizzontale non appena l'ultima colonna viene ristretta/estesa a LASTCOLUMN_MINWIDTH px)
      If BindingColumns.Count > 0 AndAlso
         BindingColumns.Any(Function(x) x.Visible = True) Then
         columnsWidth = columnsWidth - BindingColumns.Last(Function(x) x.Visible = True).ActualWidth + LASTCOLUMN_MINWIDTH
      End If
   End Sub

   Private Sub Cell_MouseRightButtonDown(sender As Object, e As RoutedEventArgs)
      Dim nDispIndex As Integer = sender.DisplayIndex
      ' creo il ContextMenu con gli item checkabili
      Dim cm As ContextMenu = New ContextMenu()
      Dim miReorder As MenuItem = New MenuItem()
      Dim miResize As MenuItem = New MenuItem()
      Dim miSort As MenuItem = New MenuItem()
      Dim sep1 As Separator = New Separator()
      Dim miVis As MenuItem = New MenuItem()
      Dim sep2 As Separator = New Separator()
      Dim miLock As MenuItem = New MenuItem()
      Dim miResetSort As MenuItem = New MenuItem()
      miReorder.Header = NameOf(EgtDataGridColumn.CanUserReorder)
      miResize.Header = NameOf(EgtDataGridColumn.CanUserResize)
      miSort.Header = NameOf(EgtDataGridColumn.CanUserSort)
      miVis.Header = "Columns Visibility"
      miLock.Header = "Lock Table"
      miResetSort.Header = "Reset Sort"
      miReorder.IsCheckable = True
      miResize.IsCheckable = True
      miSort.IsCheckable = True
      miLock.IsCheckable = True
      miResetSort.IsCheckable = False
      miReorder.StaysOpenOnClick = True
      miResize.StaysOpenOnClick = True
      miSort.StaysOpenOnClick = True
      miLock.StaysOpenOnClick = True
      miResetSort.StaysOpenOnClick = True
      ' setto il binding
      Dim miReorderBinding As Binding = New Binding(NameOf(EgtDataGridColumn.CanUserReorder))
      Dim miResizeBinding As Binding = New Binding(NameOf(EgtDataGridColumn.CanUserResize))
      Dim miSortBinding As Binding = New Binding(NameOf(EgtDataGridColumn.CanUserSort))
      miReorderBinding.Mode = BindingMode.TwoWay
      miResizeBinding.Mode = BindingMode.TwoWay
      miSortBinding.Mode = BindingMode.TwoWay
      miReorder.DataContext = BindingColumns(nDispIndex)
      miResize.DataContext = BindingColumns(nDispIndex)
      miSort.DataContext = BindingColumns(nDispIndex)
      miReorder.SetBinding(MenuItem.IsCheckedProperty, miReorderBinding)
      miResize.SetBinding(MenuItem.IsCheckedProperty, miResizeBinding)
      miSort.SetBinding(MenuItem.IsCheckedProperty, miSortBinding)
      ' setto check e enable associati a LockTable
      miLock.IsChecked = (GetPrivateProfileInt(BindingColumns(nDispIndex).ParentDataGridName, K_LOCKTABLE, 0, m_sDataGridColumnsIniFile) > 0)
      miReorder.IsChecked = If(miLock.IsChecked, False, BindingColumns(nDispIndex).CanUserReorder)
      miResize.IsChecked = If(miLock.IsChecked, False, BindingColumns(nDispIndex).CanUserResize)
      miReorder.IsEnabled = Not miLock.IsChecked
      miResize.IsEnabled = Not miLock.IsChecked
      HeaderDispIndex = nDispIndex
      AddHandler miLock.Checked, AddressOf miLock_Checked
      AddHandler miLock.Unchecked, AddressOf miLock_Unchecked
      AddHandler miResetSort.Click, AddressOf miResetSort_Click
      ' aggiungo i MenuItem al ContextMenu
      cm.Items.Add(miReorder)
      cm.Items.Add(miResize)
      cm.Items.Add(miSort)
      cm.Items.Add(sep1)
      cm.Items.Add(miVis)
      cm.Items.Add(sep2)
      cm.Items.Add(miLock)
      cm.Items.Add(miResetSort)
      ' creo e aggiungo gli item figli di Columns Visibility
      For Each Column In BindingColumns
         Dim mi As MenuItem = New MenuItem()
         mi.Header = Column.Name
         mi.IsCheckable = True
         Dim miBinding As Binding = New Binding(NameOf(EgtDataGridColumn.Visible))
         miBinding.Mode = BindingMode.TwoWay
         mi.DataContext = Column
         mi.SetBinding(MenuItem.IsCheckedProperty, miBinding)
         mi.StaysOpenOnClick = True
         mi.IsEnabled = Column.CanUserEditVisible
         miVis.Items.Add(mi)
      Next
      ' disabilito Columns Visibility se tutti i suoi figli sono disabilitati
      Dim bIsEnabled As Boolean = False
      For Each visMenuItem In miVis.Items
         bIsEnabled = visMenuItem.IsEnabled
      Next
      cm.Items(4).IsEnabled = bIsEnabled

      cm.PlacementTarget = TryCast(sender, Button)
      cm.IsOpen = True
   End Sub

   Private Sub CurrDataGrid_ColumnReordering(sender As Object, e As DataGridColumnEventArgs) Handles CurrDataGrid.ColumnReordering
      ' conservo il vecchio indice in modo da effettuare lo scambio in BindingColumns una volta riordinate graficamente
      OldIndex = e.Column.DisplayIndex
   End Sub

   Private Sub CurrDataGrid_ColumnReordered(sender As Object, e As DataGridColumnEventArgs) Handles CurrDataGrid.ColumnReordered
      ' scambio le colonne in BindingColumns
      BindingColumns.Move(OldIndex, e.Column.DisplayIndex)
      ' scrivo tutte le colonne interessate dallo spostamento
      ' (se OldIndex > e.Column.DisplayIndex, il ciclo non viene eseguito quindi lo faccio nell'altro senso)
      For index = OldIndex To e.Column.DisplayIndex
         BindingColumns(index).SaveDataGridColumn()
      Next
      For index = e.Column.DisplayIndex To OldIndex
         BindingColumns(index).SaveDataGridColumn()
      Next
   End Sub

   Private Sub miLock_Checked(sender As Object, e As RoutedEventArgs)
      Dim miLock = DirectCast(sender, MenuItem)
      '  disabilito Reorder e Resize di tutte le colonne salvandone prima il valore
      For Each Column In BindingColumns
         Column.OldCanUserReorder = Column.CanUserReorder
         Column.OldCanUserResize = Column.CanUserResize
         Column.CanUserReorder = False
         Column.CanUserResize = False
      Next
      ' aggiorno il ContextMenu correntemente aperto
      Dim miReorder As MenuItem = TryCast(miLock.Parent, ContextMenu).Items(0)
      Dim miResize As MenuItem = TryCast(miLock.Parent, ContextMenu).Items(1)
      miReorder.IsChecked = False
      miResize.IsChecked = False
      miReorder.IsEnabled = False
      miResize.IsEnabled = False
      ' scrivo il valore di LockTable nell'INI
      WritePrivateProfileString(BindingColumns(HeaderDispIndex).ParentDataGridName, K_LOCKTABLE, 1, m_sDataGridColumnsIniFile)
   End Sub

   Private Sub miLock_Unchecked(sender As Object, e As RoutedEventArgs)
      Dim miLock = DirectCast(sender, MenuItem)
      '  abilito Reorder e Resize di tutte le colonne ripristinando il valore precedente, se presente
      For Each Column In BindingColumns
         If Not IsNothing(Column.OldCanUserReorder) Then Column.CanUserReorder = Column.OldCanUserReorder
         If Not IsNothing(Column.OldCanUserResize) Then Column.CanUserResize = Column.OldCanUserResize
      Next
      ' aggiorno il ContextMenu correntemente aperto
      Dim miReorder As MenuItem = TryCast(miLock.Parent, ContextMenu).Items(0)
      Dim miResize As MenuItem = TryCast(miLock.Parent, ContextMenu).Items(1)
      miReorder.IsChecked = If(IsNothing(BindingColumns(HeaderDispIndex).OldCanUserReorder), BindingColumns(HeaderDispIndex).CanUserReorder, BindingColumns(HeaderDispIndex).OldCanUserReorder)
      miResize.IsChecked = If(IsNothing(BindingColumns(HeaderDispIndex).OldCanUserResize), BindingColumns(HeaderDispIndex).CanUserResize, BindingColumns(HeaderDispIndex).OldCanUserResize)
      miReorder.IsEnabled = True
      miResize.IsEnabled = True
      ' scrivo il valore di LockTable nell'INI
      WritePrivateProfileString(BindingColumns(HeaderDispIndex).ParentDataGridName, K_LOCKTABLE, 0, m_sDataGridColumnsIniFile)
   End Sub

   Private Sub miResetSort_Click(sender As Object, e As RoutedEventArgs)
      ' resetto le SortDirection di tutte le colonne della datagrid
      ResetSortDirections()
   End Sub

   Private Sub CurrDataGrid_Loaded(sender As Object, e As RoutedEventArgs) Handles CurrDataGrid.Loaded
      ' setto la SortDirection, se attiva, sulla colonna su cui è attiva
      Dim colIndex As Integer = 0
      For Each col In BindingColumns
         If col.SortDirection > 0 Then
            CurrDataGrid.Items.SortDescriptions.Add(New SortDescription(CurrDataGrid.Columns(colIndex).SortMemberPath, col.SortDirection - 1))
            Exit For
         End If
         colIndex += 1
      Next
      ' calcolo la somma delle larghezze delle colonne visibili
      For Each col In BindingColumns
         If col.ColumnVisibility = Visibility.Visible Then
            columnsWidth += col.ActualWidth
         End If
      Next
   End Sub

   Private Sub CurrDataGrid_Sorting(sender As Object, e As DataGridSortingEventArgs) Handles CurrDataGrid.Sorting
      ' salvo la SortDirection corrente della colonna prima di resettarla per tutte le colonne
      Dim CurrSortDirection As ListSortDirection? = e.Column.SortDirection
      ResetSortDirections(True)
      ' ora applico la SortDirection corretta
      Select Case CurrSortDirection
         Case ListSortDirection.Ascending
            BindingColumns(e.Column.DisplayIndex).SortDirection = 2 ' la setto a Descending
         Case ListSortDirection.Descending
            BindingColumns(e.Column.DisplayIndex).SortDirection = 1 ' la setto a Ascending
         Case Else
            BindingColumns(e.Column.DisplayIndex).SortDirection = 1 ' la setto a Ascending
      End Select
   End Sub

   Private Sub ResetSortDirections(Optional bUserClick As Boolean = False)
      If bUserClick Then
         ' nel caso di click dell'utente sulla colonna da sortare occorre solamente settare a 0 la SortDirection di tutte le colonne
         For Each column In BindingColumns
            column.SortDirection = 0
         Next
      Else
         ' altrimenti (caso ResetSort da ContextMenu) è necessatio un Clear più ampio e anche grafico
         CurrDataGrid.Items.SortDescriptions.Clear()
         For Each column In BindingColumns
            column.SortDirection = 0
            ' resetto graficamente anche l'header della colonna
            CurrDataGrid.Columns(column.DisplayIndex).SortDirection = Nothing
         Next
         RaiseEvent ResetSort()
      End If
   End Sub

End Class

Public Class EgtDataGridColumn

   Private m_dgColumn As DataGridColumn

   Private Property m_ParentDataGridName As String
   Public Property ParentDataGridName As String
      Get
         Return m_ParentDataGridName
      End Get
      Set(value As String)
         m_ParentDataGridName = value
      End Set
   End Property

   Private Property m_Name As String
   Public Property Name As String
      Get
         Return m_Name
      End Get
      Set(value As String)
         m_Name = value
      End Set
   End Property

   Private Property m_Width As DataGridLength
   Public Property Width As DataGridLength
      Get
         Return m_dgColumn.Width
      End Get
      Set(value As DataGridLength)
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.Width = value
         Else
            m_Width = value
         End If
      End Set
   End Property

   Public ReadOnly Property ActualWidth As Double
      Get
         Return m_dgColumn.ActualWidth
      End Get
   End Property

   Private Property m_DisplayIndex As Integer
   Public Property DisplayIndex As Integer
      Get
         Return m_dgColumn.DisplayIndex
      End Get
      Set(value As Integer)
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.DisplayIndex = value
         Else
            m_DisplayIndex = value
         End If
      End Set
   End Property

   Private Property m_OrigIsReadOnly As Boolean = True
   Private Property m_IsReadOnly As Boolean = True
   Public Property IsReadOnly As Boolean
      Get
         Return m_dgColumn.IsReadOnly
      End Get
      Set(value As Boolean)
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.IsReadOnly = value
         Else
            m_IsReadOnly = value
         End If
      End Set
   End Property
   Public Sub ResetToOrigIsReadOnly()
      IsReadOnly = m_OrigIsReadOnly
   End Sub

   Friend OldCanUserReorder As Boolean?
   Private m_CanUserReorder As Boolean
   Public Property CanUserReorder As Boolean
      Get
         Return m_dgColumn.CanUserReorder
      End Get
      Set(value As Boolean)
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.CanUserReorder = value
         Else
            m_CanUserReorder = value
         End If
         SaveDataGridColumn()
      End Set
   End Property

   Friend OldCanUserResize As Boolean?
   Private m_CanUserResize As Boolean
   Public Property CanUserResize As Boolean
      Get
         Return m_dgColumn.CanUserResize
      End Get
      Set(value As Boolean)
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.CanUserResize = value
         Else
            m_CanUserResize = value
         End If
         SaveDataGridColumn()
      End Set
   End Property

   Private m_CanUserSort As Boolean
   Public Property CanUserSort As Boolean
      Get
         Return m_dgColumn.CanUserSort
      End Get
      Set(value As Boolean)
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.CanUserSort = value
         Else
            m_CanUserSort = value
         End If
         SaveDataGridColumn()
      End Set
   End Property

   Private m_Visible As Boolean
   Public Property Visible As Boolean
      Get
         Return m_Visible
      End Get
      Set(value As Boolean)
         m_Visible = value
         ColumnVisibility = If(m_Visible, Visibility.Visible, Visibility.Collapsed)
         SaveDataGridColumn()
      End Set
   End Property

   Private m_ColumnVisibility As Visibility
   Public Property ColumnVisibility As Visibility
      Get
         Return If(m_Visible, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_ColumnVisibility = value
         If Not IsNothing(m_dgColumn) Then
            m_dgColumn.Visibility = value
            ' ricavo la EgtDataGrid a cui appartiene la DataGridColumn
            Dim propertyInfo As Reflection.PropertyInfo = m_dgColumn.GetType().GetProperty("DataGridOwner",
                                                                               Reflection.BindingFlags.Instance Or
                                                                               Reflection.BindingFlags.NonPublic)
            Dim dg As EgtDataGrid = propertyInfo.GetValue(m_dgColumn, Nothing)
            ' ogni colonna visibile con UnitType a Star tranne l'ultima verrà settata a Pixel
            For Each bc In dg.BindingColumns
               If bc.Width.IsStar And Not bc.Name = dg.BindingColumns.Last(Function(x) x.Visible = True).Name Then
                  bc.Width = New DataGridLength(bc.ActualWidth, DataGridLengthUnitType.Pixel)
               End If
            Next
         End If
      End Set
   End Property

   Private m_CanUserEditVisible As Boolean
   Public Property CanUserEditVisible As Boolean
      Get
         Return m_CanUserEditVisible
      End Get
      Set(value As Boolean)
         m_CanUserEditVisible = value
         SaveDataGridColumn()
      End Set
   End Property

   Private m_SortDirection As Forms.SortOrder = Forms.SortOrder.None
   Public Property SortDirection As Forms.SortOrder
      Get
         Return m_SortDirection 'If(IsNothing(m_dgColumn.SortDirection), 0, m_dgColumn.SortDirection + 1)
      End Get
      Set(value As Forms.SortOrder)
         'If Not IsNothing(m_dgColumn) Then
         '   If value = 0 Then
         '      m_dgColumn.SortDirection = Nothing
         '   Else
         '      m_dgColumn.SortDirection = value - 1
         '   End If
         'Else
         m_SortDirection = value
         'End If
         SaveDataGridColumn()
      End Set
   End Property

   Sub New(sName As String)
      m_Name = sName
   End Sub

   Sub New(sParentDG As String, sName As String, bCanUserReorder As Boolean, bCanUserResize As Boolean, bCanUserSort As Boolean, bIsReadOnly As Boolean, Width As DataGridLength, bVisible As Boolean, bCanUserEditVisible As Boolean, nSortDirection As Integer)
      m_ParentDataGridName = sParentDG
      m_Name = sName
      m_CanUserReorder = bCanUserReorder
      m_CanUserResize = bCanUserResize
      m_CanUserSort = bCanUserSort
      m_OrigIsReadOnly = bIsReadOnly
      m_IsReadOnly = bIsReadOnly
      m_Width = Width
      m_Visible = bVisible
      m_CanUserEditVisible = bCanUserEditVisible
      m_SortDirection = nSortDirection
   End Sub

   Friend Sub InitColumn(dgColumn As DataGridColumn)
      m_dgColumn = dgColumn
      m_dgColumn.CanUserReorder = m_CanUserReorder
      m_dgColumn.CanUserResize = m_CanUserResize
      m_dgColumn.CanUserSort = m_CanUserSort
      m_dgColumn.IsReadOnly = m_IsReadOnly
      'm_dgColumn.DisplayIndex = m_DisplayIndex
      m_dgColumn.Width = m_Width
      m_dgColumn.Visibility = m_ColumnVisibility
      If CInt(m_SortDirection) > 0 Then
         m_dgColumn.SortDirection = m_SortDirection - 1 'If(CInt(m_SortDirection) = 0, Nothing, m_SortDirection - 1)
      Else
         m_dgColumn.SortDirection = Nothing
      End If
   End Sub

   ' funzione per calcolare e scrivere la stringa dei parametri della colonna nell'INI
   Public Function SaveDataGridColumn() As Boolean
      Dim bOk As Boolean
      Dim sColumnParams = String.Empty
      sColumnParams = Name & "," & If(CanUserReorder, 1, 0) & "," & If(CanUserResize, 1, 0) & "," & If(CanUserSort, 1, 0) & "," & If(m_OrigIsReadOnly, 1, 0) & "," & DoubleToString(Width.Value, 6) & "," & Width.UnitType & "," & If(Visible, 1, 0) & "," & If(CanUserEditVisible, 1, 0) & "," & CInt(SortDirection)
      bOk = WriteColumnPrivateProfileParam(ParentDataGridName, DisplayIndex, sColumnParams)
      Return bOk
   End Function

End Class
