Imports System.Data.SqlClient

Public Class frmVERAGCardNew


    Dim kundenSQL As New kundenSQL
    Private bindingSDL As New BindingSource
    Private dataAdapterLKW As New SqlDataAdapter
    Dim SQL As New SQL
    Dim FUNC As New cProgramFunctions
    Dim bindingString As String
    Dim aktbtn As String = ""
    Dim dgvInitWait As Boolean = False
    Public kdnr As Integer

    Private Sub frmVERAGCardNew_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' btnFilter.Left = Panel4.Width - btnFilter.Width
        pnlFilter.AutoScroll = False
        Me.dgvSDL.DataSource = Me.bindingSDL
        Dim where = ""
         If kdnr > 0 Then
            Label2.Visible = True
        End If
        newBinding("SELECT cast(KartenNr as int) KartenNr, KundenNr, KfzKennzeichen, Fahrer, Datum, Kaution, BelegNr, Ersatzkarte, Gesperrt, Defekt, Verloren, Gestohlen, Vermerk, Erfassungsdatum, Änderungsdatum, Sachbearbeiter FROM Kartenpool WHERE KundenNr is null" & where & " ORDER BY cast(KartenNr as int)")

    End Sub

    Sub newBinding(ByVal sql As String)
        dgvInitWait = True
        ' bindingSDL = Nothing
        dgvSDL.Columns.Clear()
        txtchanged = True
        clearFilter()
        setBindingstring(sql)
        initSDLBinding()
        initAllg()
        initFilter()
        txtchanged = False
        dgvInitWait = False
        If dgvSDL.RowCount > 0 Then dgvSDL.Rows(0).Cells(0).Selected = True
        dgvSDL_SelectionChanged(dgvSDL, New EventArgs)
    End Sub


    Private Sub initSDLBinding()
        dgvInitWait = True
        Dim sqlstr As String = bindingString
        ' dgvLKW.DataSource = bindingSDL
        Dim d As New SqlDataAdapter()
        Try
            Dim connectionString As String = cSqlDb.GetSDLConnectionString

            Dim filter As String = getFilter()
            If filter <> "" Then sqlstr &= " AND " & filter

            Me.dataAdapterLKW = New SqlDataAdapter(sqlstr, connectionString)
        Catch ex As SqlException
            MessageBox.Show("Der Connection-String kann nicht verarbeitet werden. Wenden Sie sich an den Programmbetreuer.")
        End Try
        Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapterLKW)
        Dim table As New DataTable()
        Me.dataAdapterLKW.Fill(table)
        '   table.Columns("KundenNr").DefaultValue = kdNr
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        'bindingSDL.Clear()
        dgvSDL.DataBindings.Clear()
        bindingSDL.DataSource = table
        dgvInitWait = False
    End Sub

    Sub initFilter()
        dgvInitWait = True
        Dim left_value As Integer = 40 - dgvSDL.HorizontalScrollingOffset
        pnlFilter.Controls.Clear()
        Dim l As New Label
        l.Left = left_value - 38 : l.Top = 12
        l.Width = 36 : l.Text = "Filter:"
        pnlFilter.Controls.Add(l)

        If dgvSDL.Rows.Count > 0 Then
            For i As Integer = 0 To dgvSDL.ColumnCount - 1 Step 1
                Dim c As DataGridViewColumn = dgvSDL.Columns(i)
                If c.Visible Then '
                    Try
                        If c.ValueType.Name = "Boolean" Then
                            Dim t As New CheckBox
                            t.Left = left_value
                            t.Top = 10
                            t.CheckAlign = ContentAlignment.MiddleCenter
                            t.ThreeState = True
                            t.CheckState = CheckState.Indeterminate
                            t.Name = "cbxSrch_" & c.Name
                            AddHandler t.CheckStateChanged, AddressOf initSearch
                            t.Width = c.Width
                            pnlFilter.Controls.Add(t)
                        End If
                        If c.ValueType.Name = "String" Or c.ValueType.Name = "Int32" Or c.ValueType.Name = "Decimal" Then
                            Dim t As New TextBox
                            t.Left = left_value
                            t.Top = 10
                            t.Name = "txtSrch_" & c.Name
                            AddHandler t.TextChanged, AddressOf initSearch
                            t.Width = c.Width
                            pnlFilter.Controls.Add(t)
                        End If
                        If c.ValueType.Name = "DateTime" Then
                            Dim t As New TextBox
                            t.Left = left_value
                            t.Top = 10
                            t.Name = "datSrch_" & c.Name
                            AddHandler t.TextChanged, AddressOf initSearch
                            t.Width = c.Width
                            pnlFilter.Controls.Add(t)
                        End If
                        left_value += c.Width

                    Catch ex As Exception

                    End Try
                End If
            Next
        End If
        dgvInitWait = False
    End Sub
    Dim txtchanged As Boolean = False
    Sub initSearch(sender As Object, e As EventArgs)
        txtchanged = True
        initSDLBinding()
        initAllg()
        txtchanged = False
        If dgvSDL.RowCount > 0 Then dgvSDL.Rows(0).Cells(0).Selected = True
        dgvSDL_SelectionChanged(dgvSDL, New EventArgs)
    End Sub

    Function getFilter() As String
        Dim filter As String = ""
        Dim andstr As String = ""
        If pnlFilter.HasChildren Then
            For Each c In pnlFilter.Controls
                If c.GetType.Name = "TextBox" Then
                    Try
                        Dim t As TextBox = DirectCast(c, TextBox)
                        If t.Text <> "" Then
                            If t.Name.Contains("txtSrch_") Then
                                filter &= andstr & " [" & t.Name.Replace("txtSrch_", "") & "] LIKE '%" & t.Text & "%' "
                                andstr = " AND "
                            End If
                            If t.Name.Contains("datSrch_") AndAlso t.Text.Length = 10 Then
                                filter &= andstr & " ( [" & t.Name.Replace("datSrch_", "") & "] BETWEEN '" & t.Text & "' AND '" & t.Text & "' ) "
                                andstr = " AND "
                            End If

                        End If
                    Catch ex As Exception
                    End Try
                ElseIf c.GetType.Name = "CheckBox" Then
                    Try
                        Dim t As CheckBox = DirectCast(c, CheckBox)
                        If Not t.CheckState = CheckState.Indeterminate Then
                            filter &= andstr & " [" & t.Name.Replace("cbxSrch_", "") & "] = '" & t.CheckState & "' "
                            andstr = " AND "
                        End If

                    Catch ex As Exception
                    End Try
                End If
            Next
            '  Me.bindingSDL = New BindingSource
            '   Me.dgvSDL.DataSource = Me.bindingSDL
        End If
        Return filter
    End Function

    Sub clearFilter()
        If pnlFilter.HasChildren Then
            pnlFilter.Controls.Clear()
        End If
    End Sub


    Private Sub dgvSDL_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgvSDL.ColumnWidthChanged
        If Not txtchanged Then initFilter()
    End Sub

    Sub setBindingstring(ByVal b As String)
        bindingString = b
    End Sub

    Sub initAllg()
        With dgvSDL
            .RowTemplate.Height = 20
            .AllowUserToOrderColumns = False
            .AllowUserToResizeRows = False
            .Columns("Vermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("Vermerk").MinimumWidth = 150
            .Columns("Erfassungsdatum").ReadOnly = True
            .Columns("Änderungsdatum").ReadOnly = True
            .Columns("Sachbearbeiter").ReadOnly = True
            Exit Sub


            .Columns("Vermerk").MinimumWidth = 150
            .Columns("Erfassungsdatum").Visible = False
            .Columns("Änderungsdatum").Visible = False
            .Columns("Sachbearbeiter").Visible = False
            .Columns("SDLNr").Width = 50
            If Not aktbtn = "btnSDL_Alle" Then .Columns("SDLNr").Visible = False
            Try
                .Columns("SDLBez").ReadOnly = True
                .Columns("SDLBez").Width = 150
            Catch : End Try
            .Columns("Vermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("KfzKennzeichen").AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .Columns("Vermerk").Width = 250
            .Columns("Ersatzkarte").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("Gesperrt").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("Defekt").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("Verloren").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns("Gestohlen").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With

    End Sub

    Private Sub dgvLKW_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSDL.CellValueChanged
        btnUpdate.Enabled = True
    End Sub
    Private Sub dgvSDL_SelectionChanged(sender As Object, e As EventArgs) Handles dgvSDL.SelectionChanged

    End Sub

    Private Sub btnUpdate2_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Try
            Me.dataAdapterLKW.Update(CType(Me.bindingSDL.DataSource, DataTable))
            sender.Enabled = False
        Catch ex As Exception
            MsgBox(ex.Message, "Es ist ein Fehler aufgetreten:")
        End Try
    End Sub

   
    Private Sub dgvSDL_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSDL.CellDoubleClick
        If kdnr > 0 Then
            sender.rows(e.RowIndex).cells(e.ColumnIndex).value = kdnr
        End If
    End Sub
End Class
