I’m new in WPF. I try to do a DataGrid which is binded with my entity framework model table. Name of my table is: Words. Im trying to do simple dictionary.
Im using SQL Sever Compact, WPF, Entity framework.
However, after removing records from table it is working ok… rows from DataGrid desapers
Can you tell me why after adding records to entity they are not inseted to DataGrid?
After turn off program and turn on again the records are in DataGrid.
Here is my code of DataGrid window:
<Grid>
<Grid>
<DataGrid IsReadOnly="True" AutoGenerateColumns="False" Name="dataGrid" DataContext="{Binding }" ItemsSource="{Binding}" Margin="0,90,0,0">
<DataGrid.Columns>
<DataGridTextColumn Header="Sentence" Binding="{Binding Path=sentence}" />
<DataGridTextColumn Header="Translation" Binding="{Binding Path=translation}" />
<DataGridTemplateColumn Header="Operations">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" Click="Button_Click" Tag="{Binding Path=id}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="sentence" VerticalAlignment="Top" Width="153" />
<TextBox Height="23" Margin="171,12,131,0" Name="translations" VerticalAlignment="Top" AcceptsReturn="True" />
<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="90,41,0,0" Name="addBtn" VerticalAlignment="Top" Width="75" Click="addBtn_Click" />
</Grid>
Here is my c# code:
public partial class AddWordWnd : Window {
static dbEntities db = new dbEntities();
public AddWordWnd() {
InitializeComponent();
dataGrid.DataContext = db.Words;
dataGrid.Visibility = System.Windows.Visibility.Visible;
}
private void Button_Click( object sender, RoutedEventArgs e ) {
Button btn = sender as Button;
System.Guid ii = System.Guid.Parse(btn.Tag.ToString());
MessageBox.Show(ii.ToString());
db.DeleteObject(db.Words.Single(w => w.id == ii));
db.SaveChanges();
}
private void addBtn_Click( object sender, RoutedEventArgs e ) {
System.Guid gg = System.Guid.NewGuid();
db.Words.AddObject(new Words() { id = gg, sentence = translations.Text, translation = sentence.Text });
db.SaveChanges();
dataGrid.Items.Refresh();
}
}
Structure of my database table:
Words
---------------
id uniqueidentifier rowguidcol not null primary key,
sentence nvarchar(255) not null,
translation nvarchar(255) not null,
My best guess would be that the DataContext is getting set to a copy of your item, instead of pointing to the existing item.
Instead of setting the DataContext to an item, bind it to that item. This will make the DataContext point to the item, instead of copying it.
Edit
Just noticed you’re binding directly to the context. By default, that doesn’t raise the CollectionChanged event to tell your DataGrid that the items have changed, so the DataGrid doesn’t try to re-read it’s ItemsSource.
Try either refreshing the binding manually (
dataGrid.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget()), or refreshing the context by requerying the database (see this answer for an example)