private void RemoveInvoice_Click(object sender, RoutedEventArgs e)
 {
     if (invoicesDataGrid.SelectedItem is Invoice s)
     {
         using (var db = new db.InvoiceDbContext())
         {
             db.Invoices.Remove(db.Invoices.Single(x => x.Id == s.Id));
             db.SaveChanges();
             invoicesDataGrid.ItemsSource = db.Invoices.ToList();
         }
     }
     invoicesDataGrid.Items.Refresh();
 }
        public MainWindow()
        {
            InitializeComponent();

            using (var db = new db.InvoiceDbContext())
            {
                invoicesDataGrid.ItemsSource = db.Invoices.ToList();
                foreach (var col in invoicesDataGrid.Columns)
                {
                    col.CanUserSort = true;
                }
            }
        }
        private void AddInvoice_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new InvoiceWindow();

            if (dialog.ShowDialog() == true)
            {
                using (var db = new db.InvoiceDbContext())
                {
                    db.Invoices.Add(dialog.inv);
                    db.SaveChanges();
                    var dd = db.Invoices.ToList();
                    invoicesDataGrid.ItemsSource = db.Invoices.ToList();
                }
            }
        }
        private void EditInvoice_Click(object sender, RoutedEventArgs e)
        {
            if (invoicesDataGrid.SelectedItem is Invoice s)
            {
                using (var db = new db.InvoiceDbContext())
                {
                    var inv = db.Invoices.Include("Items").Single(x => x.Id == s.Id);

                    var dialog = new InvoiceWindow(inv);
                    if (dialog.ShowDialog() == true)
                    {
                        //db.Invoices.Include("items").ToList()[db.Invoices.Include("items").ToList().FindIndex(x => x.Id == s.Id)] = dialog.inv;
                        db.SaveChanges();
                        invoicesDataGrid.ItemsSource = db.Invoices.ToList();
                    }
                }
            }
        }
 private void InvoicesDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (invoicesDataGrid.SelectedItem is Invoice s)
     {
         using (var db = new db.InvoiceDbContext())
         {
             var list = db.Invoices.Include("Items").Single(x => x.Id == s.Id);
             if (list.Items != null)
             {
                 invoicesItemsDataGrid.ItemsSource = list.Items;
             }
             foreach (var col in invoicesItemsDataGrid.Columns)
             {
                 col.CanUserSort = true;
             }
         }
     }
 }