1

I'm trying to build simplest CRUD application on Windows desktop. Decided to go with WPF over Windows Forms and Lightswitch.

Created database, then generated "Code first from database" with Entity Framework. Put datagrid with one table, set datasource. Run - it worked, I can Read.

Added a button, created Click handler with context.SaveChanges(); - ok, Update works.

Now, cannot get to Create part. Added

authorsDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        var newAuthor = e.Row.Item as authors;
        context.authors.Add(newAuthor);
        context.SaveChanges();
    }
}

and unfortunately e.Row.Item (or e.Row.DataContext) comes in empty. Where could be mistake?

WPF/WinForm CRUD tutorial links are welcome; been googling for days, haven't found a good one.

avj
  • 1,626
  • 1
  • 19
  • 23

1 Answers1

0

Ok, problem is solved; although original question is not answered, I went another way - created separate button which creates and adds Author based on currently selected item:

    private void create_Click(object sender, RoutedEventArgs e)
    {
        var newAuthor = authorsDataGrid.SelectedItem as authors;
        context.authors.Add(newAuthor);
        context.SaveChanges();
        this.authorsDataGrid.Items.Refresh();
    }
avj
  • 1,626
  • 1
  • 19
  • 23