
Public Class frmZolltarif

    Dim HISTORY As New List(Of String)

    Private Sub frmZolltarif_Load(sender As Object, e As EventArgs) Handles Me.Load
        resetDGV()

    End Sub

    Sub resetDGV()
        Dim sql = "SELECT 	substring([ezttxt_code],0,3) as kapitel,[ezttxt_code],[ezttxt_hirnr],[ezttxt_cdesid],[ezttxt_datbeg],[ezttxt_datend],[ezttxt_txtart],[ezttxt_text] " &
                               "       FROM [atlas].[dbo].[ezttxt] " &
                             "         where [ezttxt_datend] > getdate() " &
                             " and [ezttxt_code] like '%000000000'     order by [ezttxt_code],[ezttxt_datend]"
        initDGV(sql)
        HISTORY = New List(Of String)
        HISTORY.Add(sql)
    End Sub
    Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        '  MsgBox(DataGridView1.Rows(e.RowIndex).Cells("ezttxt_hirnr").Value().ToString)
        If Not DataGridView1.Rows(e.RowIndex).Cells("ezttxt_txtart").Value().ToString = "H" Then Exit Sub

        Dim code As String = DataGridView1.Rows(e.RowIndex).Cells("ezttxt_code").Value().ToString
        Dim hirNr As Integer = CInt(DataGridView1.Rows(e.RowIndex).Cells("ezttxt_hirnr").Value().ToString) + 1
        Dim zeros As String = ""
        ' MsgBox(code)
        While code.ToString.EndsWith("0")
            code = code.Substring(0, code.Length - 1)
            zeros &= "0"
        End While
        If code = "" Then Exit Sub
        Dim tmpFrom = code & zeros
        Dim tmpTo = (CInt(code) + 1) & zeros

        Label1.Text = tmpFrom
        Label2.Text = tmpTo
        Label3.Text = DataGridView1.Rows(e.RowIndex).Cells("ezttxt_text").Value().ToString

        Dim sql = "SELECT 	substring([ezttxt_code],0,3) as kapitel,[ezttxt_code],[ezttxt_hirnr],[ezttxt_cdesid],[ezttxt_datbeg],[ezttxt_datend],[ezttxt_txtart],[ezttxt_text] " &
                                "       FROM [atlas].[dbo].[ezttxt] " &
                              "         where [ezttxt_datend] > getdate() " &
                              " and  convert(bigint,ezttxt_code )  > '" & tmpFrom & "' and  convert(bigint,ezttxt_code )  < '" & tmpTo & "' and ezttxt_hirnr<=" & hirNr & "   order by [ezttxt_code],[ezttxt_datend]"
        initDGV(sql)
    End Sub

    Sub initDGV(sql)
        With DataGridView1
            .DataSource = (New SQL).loadDgvBySql(sql, "ATLAS")
            HISTORY.Add(sql)

            .Columns("ezttxt_cdesid").Visible = False
            .Columns("ezttxt_datbeg").Visible = False
            .Columns("ezttxt_datend").Visible = False
            .Columns("ezttxt_txtart").Visible = False
            
            .Columns("ezttxt_hirnr").Visible = False
            .Columns("ezttxt_hirnr").Visible = False


            .Columns("kapitel").HeaderText = "Kapitel"
            .Columns("kapitel").MinimumWidth = 100

            .Columns("ezttxt_text").HeaderText = "Text"
            .Columns("ezttxt_text").MinimumWidth = 300
            .Columns("ezttxt_text").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            If False Then

                For Each r As DataGridViewRow In .Rows
                    If r.Cells("ezttxt_txtart").Value <> "H" Then
                        ' r.DefaultCellStyle.Font = New Font(Me.Font.FontFamily, Me.Font.Size, FontStyle.Italic)
                    Else
                        r.DefaultCellStyle.Font = New Font(Me.Font.FontFamily, Me.Font.Size, FontStyle.Bold)
                    End If
                Next
            End If
        End With

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        resetDGV()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If HISTORY.Count > 1 Then
            HISTORY.RemoveAt(HISTORY.Count - 1)
            initDGV(HISTORY(HISTORY.Count - 1))
        Else
            resetDGV()
        End If

    End Sub
End Class