Hi, @RogerSchlueter-7899. Welcome to Microsoft Q&A.
ocExpenses.OrderByDescending(Function(ve) ve.ChargeDate)
will return the sorted result and will not sort the source data.
The initial sorting could be done as follows:
ocExpenses = New ObservableCollection(Of Expenses)(ocExpenses.OrderByDescending(Function(ve) ve.ChargeDate))
dgExpenses.ItemsSource = ocExpenses
Dim ecv As IEditableCollectionView = dgExpenses.Items
ecv.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning
New data needs to be inserted at the top. You could refer to the following method to adjust the insertion position in the RowEditEnding
event.
<DataGrid Name="dgExpenses" AutoGenerateColumns="True" CanUserAddRows="True" RowEditEnding="dgExpenses_RowEditEnding" />
'Avoid infinite recursive calls To dgExpenses.CommitEdit(DataGridEditingUnit.Row, True) In dgExpenses_RowEditEnding
Public isEdit As Boolean = True
Private Sub dgExpenses_RowEditEnding(sender As Object, e As DataGridRowEditEndingEventArgs)
If isEdit Then
isEdit = False
'
dgExpenses.CommitEdit(DataGridEditingUnit.Row, True)
Dim newItem = ocExpenses.LastOrDefault()
If newItem IsNot Nothing Then
ocExpenses.Remove(newItem)
ocExpenses.Insert(0, newItem)
End If
isEdit = True
End If
End Sub
If you want to keep the order after insertion, you could compare the elements of ocExpenses
in dgExpenses_RowEditEnding
to find the position to be inserted and insert it.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.