Public Class usrcntlSpeisen


    Public sqlselect As String = "SELECT EssenName, EssenLastUsage, EssenCreateDate, EssenType FROM GEssen"
    Public SqlSpeiseArt As String = ""
    Public senderbox As New TextBox
    Public newMenu As Boolean = False
    Public Speise As New cEssen


    Private Sub usrcntlSpeisen_Load(sender As Object, e As EventArgs) Handles Me.Load
        LoadDGV()


    End Sub

    Function LoadDGV()
        Dim ds As New DataSet
        cSQL.SQL2DS(sqlselect & " WHERE EssenName LIKE '%" & txtSuche.Text & "%' " & SqlSpeiseArt & "ORDER BY EssenName", ds)

        With dgvSpeise
            .MultiSelect = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .DataSource = ds.Tables(0)

            .Columns("EssenName").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns("EssenCreateDate").Visible = False
            .Columns("EssenType").Visible = False
        End With

    End Function


    Private Sub dgvSpeise_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSpeise.CellContentClick, dgvSpeise.CellClick
        Speise = New cEssen
        txtSpeise.Enabled = True
        senderbox.Text = dgvSpeise.CurrentRow.Cells("Essenname").Value
        txtSpeise.Text = dgvSpeise.CurrentRow.Cells("Essenname").Value
        comSpeiseart.SelectedItem = dgvSpeise.CurrentRow.Cells("Essentype").Value

        Speise.EssenName = dgvSpeise.CurrentRow.Cells("Essenname").Value
        Speise.EssenType = dgvSpeise.CurrentRow.Cells("EssenType").Value
        Speise.EssenCreateDate = dgvSpeise.CurrentRow.Cells("EssenCreateDate").Value
        Speise.EssenLastUsage = dgvSpeise.CurrentRow.Cells("EssenLastUsage").Value

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtSuche.TextChanged
        LoadDGV()
    End Sub

    Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
        newMenu = True
        Speise = New cEssen
        txtSpeise.Enabled = True
        txtSpeise.Text = ""
        txtSuche.Text = ""

        txtSpeise.BackColor = Color.Khaki

        'Dim ding As New cMenu
        'ding.MenuTitel = "Test"
        'ding.MenuDateVon = Now.Date.AddDays(-2)
        'ding.MenuDateVon = Now.Date.AddDays(1)
        'ding.MenuDetail1 = "jupp, detail 1"

        'Dim mahl As New cEssen
        'mahl.EssenCreateDate = Now
        'mahl.EssenLastUsage = Now.Date
        'mahl.EssenName = "Yummy yummy yummy"

        'ding.MenuEssenMo = mahl.EssenName
        'ding.MenuID = Now & Now.AddDays(-2)

        'ding.saveAsNew()
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Speise.EssenName = txtSpeise.Text
        Speise.EssenType = comSpeiseart.SelectedItem

        If newMenu = True Then
            Speise.EssenCreateDate = Now
            Speise.EssenLastUsage = Now
            Speise.saveAsNew()
            newMenu = False
            txtSpeise.BackColor = Color.White
        Else
            Speise.update()
        End If

        LoadDGV()

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim Speise2Delete As New cEssen
        Speise2Delete.getEssenByCreateDate(dgvSpeise.CurrentRow.Cells("EssenCreateDate").Value)
        Speise2Delete.Delete()
        LoadDGV()
    End Sub
End Class
