Question about saving column headers in datagrid!

When I change the order of the columns in my datagrid, these settings are saved, but they are not displayed when the program is initialized. To show the saved settings, I must first select another item from the drop-down list and then return. Then the saved order appears. How can I get a column to initialize?

+3
source share
2 answers

I'm not sure I took on your question, but if the problem is an ordered data order, perhaps you can order the original datagrid collection before binding (for example, using LINQ). Sorry for VB, but I think you can easily:

This is a compact example:

Public Sub New()
    ' initialize the collection
    _MyTypeItemList = From a In MyTypeItemList Select a Order By a.MyProperty2
End Sub

. , , , , , :

Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class MyType
    Public Sub New()
        ' initialize the collection
        _MyTypeItemList = From a In MyTypeItemList Select a Order By a.MyProperty2
    End Sub

    Private _MyTypeItemList As New ObservableCollection(Of MyTypeItem)

    Public ReadOnly Property MyTypeItemList() As ObservableCollection(Of MyTypeItem)
        Get
            MyTypeItemList = _MyTypeItemList
        End Get
    End Property
End Class

Public Class MyTypeItem
    Implements INotifyPropertyChanged

    Public Sub New(ByVal MyProperty1Pass As Long, ByVal MyProperty2Pass As Date)
        _MyProperty1 = MyProperty1Pass
        _MyProperty2 = MyProperty2Pass
    End Sub

    Private _MyProperty1 As Long = Nothing
    Private _MyProperty2 As Date = Nothing

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property MyProperty1() As Long
        Get
            MyProperty1 = Me._MyProperty1
        End Get

        Set(ByVal value As Long)
            If Not Object.Equals(Me._MyProperty1, value) Then
                Me._MyProperty1 = value
                Me.OnPropertyChanged("MyProperty1")
            End If
        End Set
    End Property

    Public Property MyProperty2() As Date
        Get
            Return Me._MyProperty2
        End Get

        Set(ByVal value As Date)
            If Not Object.Equals(Me._MyProperty2, value) Then
                Me._MyProperty2 = value
                Me.OnPropertyChanged("MyProperty2")
            End If
        End Set
    End Property

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        Dim handler As PropertyChangedEventHandler = Me.PropertyChangedEvent
        If handler IsNot Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End If
    End Sub
End Class
0

datagrid ? asp.net, , gridview/datagrid, .

0

Source: https://habr.com/ru/post/1780544/


All Articles