Imports System.Linq

Public Class frmBestellungen

    Dim NWindDataKontext As New NordwindEntities

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cbxBestellungen.DisplayMember = "OrderID"
        cbxBestellungen.ValueMember = "OrderID"
        cbxBestellungen.DataSource = NWindDataKontext.Orders

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxBestellungen.SelectedIndexChanged
        DataGridAktualisieren()
    End Sub

    Private Sub butNeu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butNeu.Click
        Dim intOrderID As Integer = CType(cbxBestellungen.SelectedItem, Orders).OrderID
        Dim dlgEdit As New frmOrder(NWindDataKontext, intOrderID)
        If dlgEdit.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim odOrderDetail As New Order_Details
            With odOrderDetail
                .Quantity = dlgEdit.Anzahl
                .Discount = 0
                .Orders = CType(cbxBestellungen.SelectedItem, Orders)
                .Products = dlgEdit.Produkt
            End With
            NWindDataKontext.AddToOrder_Details(odOrderDetail)
            NWindDataKontext.SaveChanges()

            DataGridAktualisieren()
        End If
    End Sub

    Private Sub DataGridAktualisieren()
        Dim intOrderID As Integer = CType(cbxBestellungen.SelectedItem, Orders).OrderID
        Dim quBestellDetails = From BestellDetails In NWindDataKontext.Order_Details _
                               Join Produkte In NWindDataKontext.Products On BestellDetails.ProductID Equals Produkte.ProductID _
                               Where BestellDetails.OrderID = intOrderID _
                               Select New With {BestellDetails.OrderID, BestellDetails.Quantity, Produkte.ProductName, BestellDetails.UnitPrice}
        dgvBestelldetails.DataSource = quBestellDetails
    End Sub
End Class
