2

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,

1 Answer 1

2

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.

Binding b = new Binding();
b.Source = db.Words;
dataGrid.SetBinding(DataGrid.ItemsSourceProperty, b);

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)

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your reply, but it still didnt solve my problem... :/
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 manually raising the PropertyChanged event on the binding
That would be greatful if you tell me how can I do this? Im new in WPF. This is my second day when I writting something in WPF.
@nosbor I believe it is something like BindingOperations.GetBindingExpressionBase(dataGrid, DataGrid.ItemsSourceProperty).UpdateTarget();
My guess is that even though you added the item, the context won't get updated until after you requery the database. See the following link for example: stackoverflow.com/questions/3256245/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.