简体   繁体   中英

Question about saving column headers in a datagrid!

When I change the order of the columns in my datagrid, those settings are saved, but they do not show up when the program is initialized. In order to show the saved settings, i must first choose another item from the drop down box and then go back. Then the saved the order shows up. How would I get the column to show up on initialization?

do you bind the datagrid after you do the changes? in asp.net at least you have to rebind after modifying a gridview/datagrid to see the results.

I'm not sure to have undertood your question, but if the problem is the sturtup order of the data maybe you can order the source-collection of the datagrid before the binding (for example using LINQ). Sorry for the VB but I think you can undertand 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

This is a more complete example. If it's not what you need, be patient, I'm italian, I'm not a guru, and my english is so and so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM