I am trying to bind a datagrid to a list of objects and make it so that when I update an object in the list the change is reflected directly into the datagrid without needing to call a refresh.
My code is:
Public Class BindingToAListOfObjectsUpdateable
Private src As List(Of MyEFEntity)
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim db As New DbEntities
src = db.MyEFEntity.ToList()
dg.DataContext = src
dg.ItemsSource = src
db.Dispose()
End Sub
Private Sub Grid_Loaded(sender As Object, e As RoutedEventArgs)
Me.WindowState = Windows.WindowState.Maximized
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
MsgBox(src(0).Field1)
src(0).Field1 = "EDITED"
End Sub
Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
dg.Items.Refresh()
End Sub
End Class
XAML:
<Window x:Class="BindingToAListOfObjectsUpdateable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BindingToAListOfObjectsUpdateable" Height="300" Width="300">
<Grid Loaded="Grid_Loaded">
<DataGrid x:Name="dg" AutoGenerateColumns="True" Margin="0,0,0,43"/>
<Button Content="Bind Data" Click="Button_Click" Height="43" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="85"/>
<Button Content="Change First Rec" Margin="85,0,106,0" Height="43" VerticalAlignment="Bottom" Click="Button_Click_1"/>
<Button Content="Refresh DG" Height="43" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="106" Click="Button_Click_2"/>
</Grid>
</Window>
Is this possible to do without having the call the refresh?
I want to be able to edit the underlying list of objects and have the change directly reflected to my datagrid, is this possible?
Thanks