Public Class frmBonitätsverlauf

    Public kdnr = -1
    Sub New(kdnr)
        InitializeComponent()
        Me.kdnr = kdnr
    End Sub
    Sub New(kdnr As Integer, PARENT_FORMULARMANAGER As frmFormulare)
        InitializeComponent()
        Me.kdnr = kdnr
        Me.PARENT_FORMULARMANAGER = PARENT_FORMULARMANAGER
    End Sub
    Sub New(kdnr As Integer, PARENT_USRCNTL As usrcntlCreditreformBoniAuskunft)
        InitializeComponent()
        Me.kdnr = kdnr
        Me.PARENT_USRCNTL = PARENT_USRCNTL
    End Sub


    Public PARENT_FORMULARMANAGER As frmFormulare = Nothing
    Public PARENT_USRCNTL As usrcntlCreditreformBoniAuskunft = Nothing

    Private Sub frmBonitätsverlauf_Load(sender As Object, e As EventArgs) Handles Me.Load

        KdSearchBox1.initKdBox(Me)

        AddHandler KdSearchBox1.PropertyChanged, Sub()
                                                     initDGV()
                                                 End Sub

        KdSearchBox1.KdNr = kdnr

    End Sub

    Sub initDGV()
        Try
            Me.Cursor = Cursors.WaitCursor
            MyDatagridview1.Columns.Clear()
            If KdSearchBox1.KdNr_value > 0 Then
                With MyDatagridview1
                    Dim sqlstr As String = ""
                    sqlstr &= "SELECT *  FROM [tblBonitaetsauskunft] where ba_KundenNr=" & KdSearchBox1.KdNr_value & " ORDER BY ba_Datum DESC"

                    .SET_SQL(sqlstr, "FMZOLL")
                    .LOAD()


                    .RowHeadersVisible = False
                    .ReadOnly = True
                    .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    .MultiSelect = False

                    .Columns("ba_id").Visible = False
                    .Columns("ba_KundenNr").Visible = False
                    .Columns("ba_MaId").Visible = False


                    .Columns("ba_CRArt").Visible = False
                    .Columns("ba_CRAnsprechpartnerHerrFrau").Visible = False
                    .Columns("ba_CRAnsprechpartner").Visible = False
                    .Columns("ba_Firma").Visible = False
                    .Columns("ba_Adresse1").Visible = False
                    .Columns("ba_Adresse2").Visible = False
                    .Columns("ba_LetztesBearbeitungsDatum").Visible = False
                    .Columns("ba_Bankverbindung").Visible = False
                    .Columns("ba_Sonstiges").Visible = False
                    .Columns("ba_Umsatz").DefaultCellStyle.Format = "C2"
                    .Columns("ba_Hoechstkredit").DefaultCellStyle.Format = "C2"

                    For Each c As DataGridViewColumn In .Columns
                        c.HeaderText = c.Name.Replace("ba_", "")
                    Next
                    If .RowCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
                End With
            End If

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        Me.Cursor = Cursors.WaitCursor
        cProgramFunctions.genExcelFromDGV_NEW(MyDatagridview1, False)
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub FlatButton7_Click(sender As Object, e As EventArgs) Handles FlatButton7.Click
        initDGV()
    End Sub

    Private Sub MyDatagridview1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDatagridview1.CellDoubleClick
        If e.RowIndex < 0 Then Exit Sub

        If PARENT_USRCNTL IsNot Nothing Then
            PARENT_USRCNTL.LoadValues(MyDatagridview1.Rows(e.RowIndex).Cells("ba_id").Value)
            Me.Close()
        ElseIf PARENT_FORMULARMANAGER IsNot Nothing Then
            Dim found = False
            For Each c In PARENT_FORMULARMANAGER.MyFlowLayoutPanel1.Controls
                If c.GetType.ToString = GetType(usrcntlCreditreformBoniAuskunft).ToString Then
                    DirectCast(c, usrcntlCreditreformBoniAuskunft).LoadValues(MyDatagridview1.Rows(e.RowIndex).Cells("ba_id").Value)
                    found = True
                End If
            Next
            If found Then Me.Close() : Exit Sub
        Else
            Dim f As New frmFormulare
            f.Show()
            f.addForm("Boniteatsauskunft", {MyDatagridview1.Rows(e.RowIndex).Cells("ba_id").Value})

        End If
    End Sub

End Class