Search code examples
wpfvb.netdata-bindinginsertcollectionviewsource

how to insert record with wpf databinding?


im new to wpf, so please bear with me

i made a window for the purpose of managing "profile" records

while the navigation and updating is working

i cannot seem to get insertion working, as well

heres the very simple code

Imports System.Collections.ObjectModel

Public Class Window1
Dim WindowEntities As New DataEntities
Dim WindowList As ObservableCollection(Of Profile)

Function ProfilesViewSource() As CollectionViewSource
    Return CType(FindResource("ProfilesViewSource"), CollectionViewSource)
End Function
Function DefaultView() As CollectionView
    Return CollectionViewSource.GetDefaultView(ProfilesViewSource.View)
End Function

Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles MyBase.Loaded
    WindowList = New ObservableCollection(Of Profile)(From x In WindowEntities.Profiles Order By x.ProfileName)
    ProfilesViewSource.Source = WindowList
End Sub

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnSave.Click
    WindowEntities.SaveChanges()
End Sub

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnAdd.Click
    WindowList.Add(New Profile)
    DefaultView.MoveCurrentToLast()
End Sub

Private Sub btnNext_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnNext.Click
    DefaultView.MoveCurrentToNext()
End Sub
End Class

when i press add, i get a new empty record, when i fill it in, i see that the collection sees it, as there is an onform listbox showing the profiles, and it gets listed there, so its actually attached to the list, but the savechanges command does not insert it into the db

there may be some validation errors upon insert, but then i would get some indication, right? now its just silently failing. as if i never tied to commit changes

thank you very much for your help fellows


Solution

  • (by IVerzin) 1. create new Profile. 2. Add it to WindowEntities 3. Add it to WindowList