I will be doing an MVVM Project soon and I’m working a couple tutorial/examples out. How do I take the following code and connect it to a database. If I were to have a datagrid, how can I change information in the datagrid and have it automatically update? I’ll be using MS SQL. Thanks for any tips or advice.
Class Person
Property _name As Integer
Property Name As Integer
Get
Return _name
End Get
Set(value As Integer)
_name = value
RaisePropertyChanged("Name")
End Set
End Property
End class
Then in another class:
Class Collections
Public namelist As New ObservableCollection(Of Person)
namelist.Add(New Person With {.Name = Nothing})
— Then in XAML
<ObjectDataProvider x:Key="test" ObjectType="{x:Type local:Collections}"
<DataGrid ItemsSource="{Binding Source= {StaticResource test}}">
<DataGridTextColumn Header="Name" Binding="{Binding Sample, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
ect...ect....ect...
Now where or how do I connect my MS SQL table to all this?
IF you’re using MVVM, then your bindings (if wired properly) will propagate changes you make in the GridView to the properties their bound too if you wire the events when a cell edit is finished, etc.
Here’s a source on that:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=563
I’m coming from a C# only background but,
for instance if you have a property of an observable collection:
on the ViewModel, if you had bound your view (the .xaml) to the VM by setting the DataContext of the View, and you had bound this OC to a combobox changing the selectedValue on the combobox would update the SelectedValue you bound to in the .xaml.
So if you had chosen for this combobox to be bound on SelectedValue to a property:
when you selected one of the choices in the combobox would update that value on the view model and it would step into the set portion of the property.
The same goes for whatever you have bound to the GridView. If you have an ObservableCollection that your gridview binds too, you’ll need to wire up the events on the view for when you feel a change should update the ViewModel and use the ICommand to propagate the change to the VM.
Another question would be are you required to use VB? I know the frameworks that make MVVM easier like Caliburn.Micro are in C#, there must be some frameworks out there to make MVVM easy with VB. Although with what you’re trying, basic MVVM wouldn’t be that hard to setup, these frameworks usually save you time from having to wire up the bindings manually.