Option Strict Off
Option Explicit On

Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Friend Class frmCadastroNovaTarifas
    Inherits System.Windows.Forms.Form
    Dim vPrepararFlexGrid As Boolean
    Dim vQtdErros As Short
    Dim dt As DataTable
    Dim objAtbd As New entidadeAtualizaBancoDados
    Dim repAtbd As New repositorioAtualizaBancoDados
    Dim objExecAtbd As New entidadeAtualizaBancoDados
    Dim repExecAtbd As New repositorioAtualizaBancoDados

#Region "Propriedades"

    Private Sub frmCadTarifas_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Try

            vPrepararFlexGrid = False
            PrepararFlexGridGrupoA()
            PrepararFlexGridGrupoBCN()
            PrepararFlexGridGrupoBBX()
            PrepararFlexGridGrupoBBR()
            PreencheCombobox()
            vPrepararFlexGrid = True
            SetaVariaveis()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub frmCadTarifas_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try

            ' Set Me = Nothing

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub txtValorGrupoBCn_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoBCn.Leave

        Try
            AtualizaValorGrade((Me.mfgGrupoBCN), (Me.txtValorGrupoBCn))
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoBCn_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoBCn.Enter

        Try

            If vPrepararFlexGrid Then
                With Me.txtValorGrupoBCn
                    .SelectionStart = 0
                    '.SelectionLength = CInt(Len(.Text))
                    .SelectionLength = .Text.Length
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub txtValorGrupoBCn_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValorGrupoBCn.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then

                AtualizaValorGrade((Me.mfgGrupoBCN), (Me.txtValorGrupoBCn))

                With Me.mfgGrupoBCN

                    Call .Focus()

                    If .Col < .Cols - 1 Then
                        .Col = .Col + 1
                    Else
                        .Col = .FixedCols
                        If .Row < .Rows - 1 Then
                            .Row = .Row + 1
                        Else
                            .Col = .FixedCols
                            .Row = .FixedRows
                        End If
                    End If
                End With

            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then

                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtValorGrupoBCn.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If

            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub txtValorGrupoBBr_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoBBr.Enter

        Try

            If vPrepararFlexGrid Then
                With Me.txtValorGrupoBBr
                    .SelectionStart = 0
                    .SelectionLength = CInt(Len(.Text))
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoBBr_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValorGrupoBBr.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Return Then

                AtualizaValorGrade((Me.mfgGrupoBBR), (Me.txtValorGrupoBBr))

                With Me.mfgGrupoBBR

                    Call .Focus()

                    If .Col < .Cols - 1 Then
                        .Col = .Col + 1
                    Else
                        .Col = .FixedCols
                        If .Row < .Rows - 1 Then
                            .Row = .Row + 1
                        Else
                            .Col = .FixedCols
                            .Row = .FixedRows
                        End If
                    End If

                End With

            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then

                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtValorGrupoBBr.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If

            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoBBr_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoBBr.Leave

        Try
            AtualizaValorGrade((Me.mfgGrupoBBR), (Me.txtValorGrupoBBr))
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoA_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoA.Enter

        Try

            If vPrepararFlexGrid Then
                With Me.txtValorGrupoA
                    .Focus()
                    .SelectionStart = 0
                    ' .SelectionLength = CInt(Len(.Text))
                    .SelectionLength = .Text.Length
                    'Me.txtValorGrupoA.SelectionLength = Me.txtValorGrupoA.Text.Length
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoA_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValorGrupoA.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Return Then

                AtualizaValorGrade((Me.mfgGrupoA), (Me.txtValorGrupoA))

                With Me.mfgGrupoA

                    Call .Focus()

                    If .Col < .Cols - 1 Then
                        .Col = .Col + 1
                    Else
                        .Col = .FixedCols
                        If .Row < .Rows - 1 Then
                            .Row = .Row + 1
                        Else
                            .Col = .FixedCols
                            .Row = .FixedRows
                        End If
                    End If

                End With

            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then

                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtValorGrupoA.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If

            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoA_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoA.Leave

        Try
            AtualizaValorGrade((Me.mfgGrupoA), (Me.txtValorGrupoA))
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoBBx_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoBBx.Enter


        Try

            If vPrepararFlexGrid Then
                With Me.txtValorGrupoBBx
                    .SelectionStart = 0
                    .SelectionLength = CInt(Len(.Text))
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoBBx_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValorGrupoBBx.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then

                AtualizaValorGrade((Me.mfgGrupoBBX), (Me.txtValorGrupoBBx))

                With Me.mfgGrupoBBX

                    Call .Focus()

                    If .Col < .Cols - 1 Then
                        .Col = .Col + 1
                    Else
                        .Col = .FixedCols
                        If .Row < .Rows - 1 Then
                            .Row = .Row + 1
                        Else
                            .Col = .FixedCols
                            .Row = .FixedRows
                        End If
                    End If

                End With

            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then

                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtValorGrupoBBx.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If

            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtValorGrupoBBx_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorGrupoBBx.Leave

        Try
            AtualizaValorGrade((Me.mfgGrupoBBX), (Me.txtValorGrupoBBx))
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoA_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoA.ClickEvent

        Try
            mfgGrupoA_EnterCell(mfgGrupoA, New System.EventArgs())
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoA_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoA.EnterCell

        Try

            If vPrepararFlexGrid Then

                With Me.mfgGrupoA

                    Me.txtValorGrupoA.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left) + 100), VB6.TwipsToPixelsY(.CellTop + 1480), VB6.TwipsToPixelsX(.CellWidth), VB6.TwipsToPixelsY(.CellHeight))
                    Me.txtValorGrupoA.Visible = True
                    Me.txtValorGrupoA.Focus()
                    Me.txtValorGrupoA.SelectionStart = 0
                    Me.txtValorGrupoA.SelectionLength = Me.txtValorGrupoA.Text.Length
                    Me.txtValorGrupoA.Text = .get_TextMatrix(.Row, .Col)
                End With

            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoA_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) Handles mfgGrupoA.KeyPressEvent

        Try

            If vPrepararFlexGrid Then
                If eventArgs.keyAscii = System.Windows.Forms.Keys.Return Then
                    eventArgs.keyAscii = 0
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBBr_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoBBR.ClickEvent

        Try
            mfgGrupoBBr_EnterCell(mfgGrupoBBR, New System.EventArgs())
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBBr_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoBBR.EnterCell

        Try

            If vPrepararFlexGrid Then

                With Me.mfgGrupoBBR
                    Call Me.txtValorGrupoBBr.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left) + 160), VB6.TwipsToPixelsY(.CellTop + 1640), VB6.TwipsToPixelsX(.CellWidth), VB6.TwipsToPixelsY(.CellHeight))
                    Me.txtValorGrupoBBr.Visible = True
                    Me.txtValorGrupoBBr.Focus()
                    Me.txtValorGrupoBBr.SelectionStart = 0
                    Me.txtValorGrupoBBr.SelectionLength = Me.txtValorGrupoBBr.Text.Length
                    Me.txtValorGrupoBBr.Text = .get_TextMatrix(.Row, .Col)
                End With

            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBBr_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) Handles mfgGrupoBBR.KeyPressEvent

        Try

            If vPrepararFlexGrid Then
                If eventArgs.keyAscii = System.Windows.Forms.Keys.Return Then
                    eventArgs.keyAscii = 0
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBBx_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoBBX.ClickEvent

        Try
            mfgGrupoBBx_EnterCell(mfgGrupoBBX, New System.EventArgs())
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBBx_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoBBX.EnterCell

        Try

            If vPrepararFlexGrid Then

                With Me.mfgGrupoBBX
                    Call Me.txtValorGrupoBBx.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left) + 160), VB6.TwipsToPixelsY(.CellTop + 1640), VB6.TwipsToPixelsX(.CellWidth), VB6.TwipsToPixelsY(.CellHeight))
                    Me.txtValorGrupoBBx.Visible = True
                    Me.txtValorGrupoBBx.Focus()
                    Me.txtValorGrupoBBx.SelectionStart = 0
                    Me.txtValorGrupoBBx.SelectionLength = Me.txtValorGrupoBBx.Text.Length
                    Me.txtValorGrupoBBx.Text = .get_TextMatrix(.Row, .Col)
                End With

            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBBx_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) Handles mfgGrupoBBX.KeyPressEvent

        Try

            If vPrepararFlexGrid Then
                If eventArgs.keyAscii = System.Windows.Forms.Keys.Return Then
                    eventArgs.keyAscii = 0
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBCn_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoBCN.ClickEvent

        Try
            mfgGrupoBCn_EnterCell(mfgGrupoBCN, New System.EventArgs())
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBCn_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgGrupoBCN.EnterCell

        Try

            If vPrepararFlexGrid Then

                With Me.mfgGrupoBCN
                    Call Me.txtValorGrupoBCn.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left) + 160), VB6.TwipsToPixelsY(.CellTop + 1640), VB6.TwipsToPixelsX(.CellWidth), VB6.TwipsToPixelsY(.CellHeight))
                    Me.txtValorGrupoBCn.Visible = True
                    Call Me.txtValorGrupoBCn.Focus()
                    Me.txtValorGrupoBCn.Focus()
                    Me.txtValorGrupoBCn.SelectionStart = 0
                    Me.txtValorGrupoBCn.SelectionLength = Me.txtValorGrupoBCn.Text.Length
                    Me.txtValorGrupoBCn.Text = .get_TextMatrix(.Row, .Col)
                End With

            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgGrupoBCn_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) Handles mfgGrupoBCN.KeyPressEvent

        Try

            If vPrepararFlexGrid Then
                If eventArgs.keyAscii = System.Windows.Forms.Keys.Return Then
                    eventArgs.keyAscii = 0
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbData_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbData.Leave

        Try

            With Me.cmbData
                .Text = FData(.Text)
                If CShort(Len(.Text)) > 0 Then
                    If Not IsDate(.Text) Then
                        MessageBox.Show("O valor informado não é uma data válida!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        .Text = String.Empty
                    End If
                Else
                    .SelectedIndex = 0
                End If
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbData_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cmbData.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii <> System.Windows.Forms.Keys.Help Then
                    KeyAscii = 0
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click

        Try

            lClose()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdLocalizar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizar.Click

        Dim lstrCod As String
        Dim llngRow As Integer
        Dim llngCol As Integer
        Dim ldblVlr As Double
        Dim ldblVAm As Double
        Dim ldblVVm As Double
        Dim ldblVBe As Double
        Dim ldblVBs As Double
        Dim ldblFio As Double

        Dim vlist

        Dim objTarifa As New entidadeTarifa
        Dim repositorioTarifa As New repositorioTarifa

        Try

            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

            LigDes(False)

            vPrepararFlexGrid = False

            Me.txtValorGrupoA.Visible = False
            Me.txtValorGrupoBCn.Visible = False
            Me.txtValorGrupoBBx.Visible = False
            Me.txtValorGrupoBBr.Visible = False

            PrepararFlexGridGrupoA()
            PrepararFlexGridGrupoBCN()
            PrepararFlexGridGrupoBBX()
            PrepararFlexGridGrupoBBR()

            vPrepararFlexGrid = True

            Me.lblInfo.Text = " Aguarde buscando as tarifas..."

            Me.lblInfo.Refresh()

            objTarifa.REFERENCIA = Convert.ToDateTime(cmbData.Text)

            vlist = repositorioTarifa.Select_3(objTarifa)

            If vlist.Count > 0 Then

                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, vlist.Count)

                For Each objTarifaFor As entidadeTarifa In vlist

                    lstrCod = Trim(IIf(IsDBNull(objTarifaFor.NUM), "", objTarifaFor.NUM))
                    ldblVlr = Convert.ToDouble(IIf(IsDBNull(objTarifaFor.VALOR), 0, objTarifaFor.VALOR))
                    ldblVAm = Convert.ToDouble(IIf(IsDBNull(objTarifaFor.VALOR_AMARELA), 0, objTarifaFor.VALOR_AMARELA))
                    ldblVVm = Convert.ToDouble(IIf(IsDBNull(objTarifaFor.VALOR_VERMELHA), 0, objTarifaFor.VALOR_VERMELHA))
                    ldblVBe = Convert.ToDouble(IIf(IsDBNull(objTarifaFor.VALOR_BASE_ECONOMICA), 0, objTarifaFor.VALOR_BASE_ECONOMICA))
                    ldblVBs = Convert.ToDouble(IIf(IsDBNull(objTarifaFor.VALOR_SEM_SUBSIDIO), 0, objTarifaFor.VALOR_SEM_SUBSIDIO))
                    ldblFio = Convert.ToDouble(IIf(IsDBNull(objTarifaFor.VALOR_TUSD_FIO_B), 0, objTarifaFor.VALOR_TUSD_FIO_B))

                    If Microsoft.VisualBasic.Left(lstrCod, 3) = "01A" Then

                        If Microsoft.VisualBasic.Left(lstrCod, 4) = "01AU" Then

                            PegaRowColGrade((Me.mfgGrupoACodigos), lstrCod, llngCol, llngRow, Microsoft.VisualBasic.Left(Replace(lstrCod, "01AU", "01BU"), 4))

                            If llngCol > 0 And llngRow > 0 Then

                                Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol, Format(ldblVlr, "#,##0.00"))
                                If ldblVAm <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, 7, Format(ldblVAm, "#,##0.00"))
                                If ldblVVm <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, 8, Format(ldblVVm, "#,##0.00"))
                                If ldblVBe <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 5, Format(ldblVBe, "#,##0.00"))
                                If ldblVBs <> 0 And llngCol > 3 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 8, Format(ldblVBs, "#,##0.00"))
                                If ldblFio <> 0 And llngCol > 3 And llngCol < 6 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 11, Format(ldblFio, "#,##0.00"))
                                TotalizaValorGrade((Me.mfgGrupoA), llngRow)
                                llngRow = 0
                                llngCol = 0

                            End If

                        Else

                            PegaRowColGrade((Me.mfgGrupoACodigos), lstrCod, llngCol, llngRow, Microsoft.VisualBasic.Left(lstrCod, 6))

                            If llngCol > 0 And llngRow > 0 Then

                                Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol, Format(ldblVlr, "#,##0.00"))
                                If ldblVAm <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, 7, Format(ldblVAm, "#,##0.00"))
                                If ldblVVm <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, 8, Format(ldblVVm, "#,##0.00"))
                                If ldblVBe <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 5, Format(ldblVBe, "#,##0.00"))
                                If ldblVBs <> 0 And llngCol > 3 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 8, Format(ldblVBs, "#,##0.00"))
                                If ldblFio <> 0 And llngCol > 3 And llngCol < 6 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 11, Format(ldblFio, "#,##0.00"))
                                TotalizaValorGrade((Me.mfgGrupoA), llngRow)
                                llngRow = 0
                                llngCol = 0

                            End If

                        End If

                    ElseIf Microsoft.VisualBasic.Left(lstrCod, 3) = "01B" Then

                        If Microsoft.VisualBasic.Left(lstrCod, 4) = "01BU" Then

                            PegaRowColGrade((Me.mfgGrupoACodigos), lstrCod, llngCol, llngRow, Microsoft.VisualBasic.Left(lstrCod, 4))

                            If llngCol > 0 And llngRow > 0 Then

                                Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol, Format(ldblVlr, "#,##0.00"))
                                If ldblVAm <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, 7, Format(ldblVAm, "#,##0.00"))
                                If ldblVVm <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, 8, Format(ldblVVm, "#,##0.00"))
                                If ldblVBe <> 0 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 5, Format(ldblVBe, "#,##0.00"))
                                If ldblVBs <> 0 And llngCol > 3 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 8, Format(ldblVBs, "#,##0.00"))
                                If ldblFio <> 0 And llngCol > 3 And llngCol < 6 Then Me.mfgGrupoA.set_TextMatrix(llngRow, llngCol + 11, Format(ldblFio, "#,##0.00"))
                                TotalizaValorGrade((Me.mfgGrupoA), llngRow)
                                llngRow = 0
                                llngCol = 0

                            End If

                        Else

                            Select Case Microsoft.VisualBasic.Left(lstrCod, 6)

                                Case "01B101", "01B201", "01B202", "01B203", "01B310", "01B401", "01B402"

                                    PegaRowColGrade((Me.mfgGrupoBCNCodigos), lstrCod, llngCol, llngRow, Microsoft.VisualBasic.Left(lstrCod, 6))

                                    If llngCol > 0 And llngRow > 0 Then

                                        Me.mfgGrupoBCN.set_TextMatrix(llngRow, llngCol, Format(ldblVlr, "#,##0.00"))
                                        Me.mfgGrupoBCN.set_TextMatrix(llngRow, 7, Format(ldblVAm, "#,##0.00"))
                                        Me.mfgGrupoBCN.set_TextMatrix(llngRow, 8, Format(ldblVVm, "#,##0.00"))
                                        Me.mfgGrupoBCN.set_TextMatrix(llngRow, llngCol + 5, Format(ldblVBe, "#,##0.00"))
                                        If ldblVBs <> 0 And llngCol > 3 Then Me.mfgGrupoBCN.set_TextMatrix(llngRow, llngCol + 8, Format(ldblVBs, "#,##0.00"))
                                        If ldblFio <> 0 And llngCol > 3 And llngCol < 6 Then Me.mfgGrupoBCN.set_TextMatrix(llngRow, llngCol + 11, Format(ldblFio, "#,##0.00"))
                                        TotalizaValorGrade((Me.mfgGrupoBCN), llngRow)
                                        llngRow = 0
                                        llngCol = 0

                                    End If

                                Case "01B102"

                                    PegaRowColGrade((Me.mfgGrupoBBXCodigos), lstrCod, llngCol, llngRow, Microsoft.VisualBasic.Left(lstrCod, 6))

                                    If llngCol > 0 And llngRow > 0 Then

                                        Me.mfgGrupoBBX.set_TextMatrix(llngRow, llngCol, Format(ldblVlr, "#,##0.00"))
                                        Me.mfgGrupoBBX.set_TextMatrix(llngRow, 7, Format(ldblVAm, "#,##0.00"))
                                        Me.mfgGrupoBBX.set_TextMatrix(llngRow, 8, Format(ldblVVm, "#,##0.00"))
                                        Me.mfgGrupoBBX.set_TextMatrix(llngRow, llngCol + 5, Format(ldblVBe, "#,##0.00"))
                                        If ldblVBs <> 0 And llngCol > 3 Then Me.mfgGrupoBBX.set_TextMatrix(llngRow, llngCol + 8, Format(ldblVBs, "#,##0.00"))
                                        If ldblFio <> 0 And llngCol > 3 And llngCol < 6 Then Me.mfgGrupoBBX.set_TextMatrix(llngRow, llngCol + 11, Format(ldblFio, "#,##0.00"))
                                        TotalizaValorGrade((Me.mfgGrupoBBX), llngRow)
                                        llngRow = 0
                                        llngCol = 0

                                    End If

                                Case "01B191", "01B291", "01B292", "01B293", "01B391"

                                    PegaRowColGrade((Me.mfgGrupoBBRCodigos), lstrCod, llngCol, llngRow, Microsoft.VisualBasic.Left(lstrCod, 6))

                                    If llngCol > 0 And llngRow > 0 Then

                                        Me.mfgGrupoBBR.set_TextMatrix(llngRow, llngCol, Format(ldblVlr, "#,##0.00"))
                                        Me.mfgGrupoBBR.set_TextMatrix(llngRow, 7, Format(ldblVAm, "#,##0.00"))
                                        Me.mfgGrupoBBR.set_TextMatrix(llngRow, 8, Format(ldblVVm, "#,##0.00"))
                                        Me.mfgGrupoBBR.set_TextMatrix(llngRow, llngCol + 5, Format(ldblVBe, "#,##0.00"))
                                        If ldblVBs <> 0 And llngCol > 3 Then Me.mfgGrupoBBR.set_TextMatrix(llngRow, llngCol + 8, Format(ldblVBs, "#,##0.00"))
                                        If ldblFio <> 0 And llngCol > 3 And llngCol < 6 Then Me.mfgGrupoBBR.set_TextMatrix(llngRow, llngCol + 11, Format(ldblFio, "#,##0.00"))
                                        TotalizaValorGrade((Me.mfgGrupoBBR), llngRow)
                                        llngRow = 0
                                        llngCol = 0

                                    End If

                            End Select

                        End If

                    End If

                    BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)

                Next

            End If

            Me.lblInfo.Text = String.Empty
            Me.lblInfo.Refresh()

            LigDes(True)
            Me.Cursor = System.Windows.Forms.Cursors.Default


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click

        Dim lblnExt As Boolean

        Try

            vQtdErros = 0

            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

            LigDes(False)

            lblnExt = VerificaSeExiste()

            If lblnExt Then
                lblnExt = (MessageBox.Show("Já existe tarifas cadastrada em " & Format(Convert.ToDateTime(cmbData.Text), "dd/MM/yyyy") & "! Deseja continuar mesmo assim?", "Confirmação de Inclusão", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) = DialogResult.Yes)
            Else
                lblnExt = True
            End If

            If lblnExt Then

                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, 4)
                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Grupo A ")
                MontaTarifa((Me.mfgGrupoACodigos), (Me.mfgGrupoA))
                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Grupo B Convenional ")
                MontaTarifa((Me.mfgGrupoBCNCodigos), (Me.mfgGrupoBCN))
                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Grupo B Baixa Renda ")
                MontaTarifa((Me.mfgGrupoBBXCodigos), (Me.mfgGrupoBBX))
                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Grupo B Branca")
                MontaTarifa((Me.mfgGrupoBBRCodigos), (Me.mfgGrupoBBR))

            End If


            If vQtdErros = 0 Then

                Me.lblInfo.Text = String.Empty
                Me.lblInfo.Refresh()

                If lblnExt Then MessageBox.Show("Gravação concluída com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Else
                MessageBox.Show("Gravação concluída com erros, favor verificar a digitação.", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

            LigDes(True)

            Me.Cursor = System.Windows.Forms.Cursors.Default

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdComposicao_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdComposicao.Click

        Try
            frmCadastroComposicaoTarifa.SetaParametros(Me)
            frmCadastroComposicaoTarifa.Show()
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub frmCadTarifas_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Escape Then
                cmdFechar_Click(cmdFechar, New System.EventArgs())
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub


#End Region

#Region "Métodos"

    Private Sub PrepararFlexGridGrupoA()

        Try

            With Me.mfgGrupoA
                Call .Clear()
                .Cols = 17
                .Rows = 18
                .FixedRows = 3
                .FixedCols = 4
                .MergeCells = MSFlexGridLib.MergeCellsSettings.flexMergeFree
                .set_ColWidth(0, 1400)
                .set_ColWidth(1, 750)
                .set_ColWidth(2, 1000)
                .set_ColWidth(3, 1000)
                .set_ColWidth(4, 1000)
                .set_ColWidth(5, 1000)
                .set_ColWidth(6, 1000)
                .set_ColWidth(7, 1000)
                .set_ColWidth(8, 1000)
                .set_ColWidth(9, 1000)
                .set_ColWidth(10, 1000)
                .set_ColWidth(11, 1000)
                .set_ColWidth(12, 1000)
                .set_ColWidth(13, 1000)
                .set_ColWidth(14, 1000)
                .set_ColWidth(15, 1000)
                .set_ColWidth(16, 1000)
                'Primeira Linha
                .Row = 0
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 7
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 8
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Segunda Linha
                .Row = 1
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Terceira Linha
                .Row = 2
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                '
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(9, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(10, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(11, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(12, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(13, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(14, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(15, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(16, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                '
                .set_TextMatrix(0, 0, "MODALIDADE")
                .set_TextMatrix(1, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(2, 0, .get_TextMatrix(0, 0))
                '
                .set_TextMatrix(3, 0, "AZUL")
                .set_TextMatrix(4, 0, .get_TextMatrix(3, 0))
                .set_TextMatrix(5, 0, "AZUL APE")
                .set_TextMatrix(6, 0, .get_TextMatrix(5, 0))
                .set_TextMatrix(7, 0, "VERDE")
                .set_TextMatrix(8, 0, .get_TextMatrix(7, 0))
                .set_TextMatrix(9, 0, .get_TextMatrix(7, 0))
                .set_TextMatrix(10, 0, "VERDE APE")
                .set_TextMatrix(11, 0, .get_TextMatrix(10, 0))
                .set_TextMatrix(12, 0, .get_TextMatrix(10, 0))
                .set_TextMatrix(13, 0, "CONVENCIONAL")
                .set_TextMatrix(14, 0, "GERAÇÃO")
                .set_TextMatrix(15, 0, "SUPRIMENTO")
                .set_TextMatrix(16, 0, "SUPR.HORO")
                .set_TextMatrix(17, 0, .get_TextMatrix(16, 0))
                '
                .set_TextMatrix(0, 1, "POSTO")
                .set_TextMatrix(1, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 1, .get_TextMatrix(0, 1))
                '
                .set_TextMatrix(3, 1, "P")
                .set_TextMatrix(4, 1, "FP")
                .set_TextMatrix(5, 1, "P")
                .set_TextMatrix(6, 1, "FP")
                .set_TextMatrix(7, 1, "NA")
                .set_TextMatrix(8, 1, "P")
                .set_TextMatrix(9, 1, "FP")
                .set_TextMatrix(10, 1, "NA")
                .set_TextMatrix(11, 1, "P")
                .set_TextMatrix(12, 1, "FP")
                .set_TextMatrix(13, 1, "")
                .set_TextMatrix(14, 1, "")
                .set_TextMatrix(15, 1, "")
                .set_TextMatrix(16, 1, "P")
                .set_TextMatrix(17, 1, "FP")
                '    '
                .set_TextMatrix(0, 2, "TUSD+TE")
                .set_TextMatrix(0, 3, .get_TextMatrix(0, 2))
                .set_TextMatrix(1, 2, "DEMANDA")
                .set_TextMatrix(1, 3, "ENERGIA")
                .set_TextMatrix(2, 2, "(R$/kW)")
                .set_TextMatrix(2, 3, "(R$/MWh)")
                '
                .set_TextMatrix(0, 4, "TUSD")
                .set_TextMatrix(0, 5, .get_TextMatrix(0, 4))
                .set_TextMatrix(1, 4, "DEMANDA")
                .set_TextMatrix(1, 5, "ENERGIA")
                .set_TextMatrix(2, 4, "(R$/kW)")
                .set_TextMatrix(2, 5, "(R$/MWh)")
                '
                .set_TextMatrix(0, 6, "TE")
                .set_TextMatrix(0, 7, .get_TextMatrix(0, 6))
                .set_TextMatrix(0, 8, .get_TextMatrix(0, 6))
                .set_TextMatrix(1, 6, "BVD")
                .set_TextMatrix(2, 6, "(R$/MWh)")
                .set_TextMatrix(1, 7, "BAM")
                .set_TextMatrix(2, 7, "(R$/MWh)")
                .set_TextMatrix(1, 8, "BVM")
                .set_TextMatrix(2, 8, "(R$/MWh)")
                '
                .set_TextMatrix(0, 9, "BASE ECONÔMICA")
                .set_TextMatrix(0, 10, .get_TextMatrix(0, 9))
                .set_TextMatrix(0, 11, .get_TextMatrix(0, 9))
                .set_TextMatrix(1, 9, "TUSD")
                .set_TextMatrix(1, 10, .get_TextMatrix(1, 9))
                .set_TextMatrix(1, 11, "TE")
                .set_TextMatrix(2, 9, "(R$/kW)")
                .set_TextMatrix(2, 10, "(R$/MWh)")
                .set_TextMatrix(2, 11, "(R$/MWh)")
                '
                .set_TextMatrix(0, 12, "SEM SUBSÍDIO")
                .set_TextMatrix(0, 13, .get_TextMatrix(0, 12))
                .set_TextMatrix(0, 14, .get_TextMatrix(0, 12))
                .set_TextMatrix(1, 12, "TUSD")
                .set_TextMatrix(1, 13, .get_TextMatrix(1, 12))
                .set_TextMatrix(1, 14, "TE")
                .set_TextMatrix(2, 12, "(R$/kW)")
                .set_TextMatrix(2, 13, "(R$/MWh)")
                .set_TextMatrix(2, 14, "(R$/MWh)")
                '
                .set_TextMatrix(0, 15, "FIO - B")
                .set_TextMatrix(0, 16, .get_TextMatrix(0, 15))
                .set_TextMatrix(1, 15, "TUSD")
                .set_TextMatrix(1, 16, .get_TextMatrix(1, 15))
                .set_TextMatrix(2, 15, "(R$/kW)")
                .set_TextMatrix(2, 16, "(R$/MWh)")
                '
                .set_MergeCol(0, True)
                .set_MergeCol(1, True)
                .set_MergeRow(0, True)
                .set_MergeRow(1, True)
                '
                .Row = 3
                .Col = 4
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PrepararFlexGridGrupoBCN()

        Try

            With Me.mfgGrupoBCN
                Call .Clear()
                .Cols = 17
                .Rows = 10
                .FixedRows = 3
                .FixedCols = 4
                .MergeCells = MSFlexGridLib.MergeCellsSettings.flexMergeFree
                .set_ColWidth(0, 1400)
                .set_ColWidth(1, 750)
                .set_ColWidth(2, 1000)
                .set_ColWidth(3, 1000)
                .set_ColWidth(4, 1000)
                .set_ColWidth(5, 1000)
                .set_ColWidth(6, 1000)
                .set_ColWidth(7, 1000)
                .set_ColWidth(8, 1000)
                .set_ColWidth(9, 1000)
                .set_ColWidth(10, 1000)
                .set_ColWidth(11, 1000)
                .set_ColWidth(12, 1000)
                .set_ColWidth(13, 1000)
                .set_ColWidth(14, 1000)
                .set_ColWidth(15, 1000)
                .set_ColWidth(16, 1000)
                'Primeira Linha
                .Row = 0
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 7
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 8
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Segunda Linha
                .Row = 1
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Terceira Linha
                .Row = 2
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                '
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(9, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(10, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(11, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(12, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(13, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(14, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(15, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(16, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                '
                .set_TextMatrix(0, 2, "TUSD+TE")
                .set_TextMatrix(0, 3, .get_TextMatrix(0, 2))
                .set_TextMatrix(0, 4, "TUSD")
                .set_TextMatrix(0, 5, .get_TextMatrix(0, 4))
                .set_TextMatrix(0, 6, "TE")
                .set_TextMatrix(0, 7, .get_TextMatrix(0, 6))
                .set_TextMatrix(0, 8, .get_TextMatrix(0, 6))
                '
                .set_TextMatrix(1, 2, "DEMANDA")
                .set_TextMatrix(1, 3, "ENERGIA")
                .set_TextMatrix(1, 4, "DEMANDA")
                .set_TextMatrix(1, 5, "ENERGIA")
                .set_TextMatrix(1, 6, "BVD")
                .set_TextMatrix(1, 7, "BAM")
                .set_TextMatrix(1, 8, "BVM")
                '
                .set_TextMatrix(0, 0, "SUBCLASSE")
                .set_TextMatrix(1, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(2, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(0, 1, "SUB")
                .set_TextMatrix(1, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 2, "(R$/kW)")
                .set_TextMatrix(2, 3, "(R$/MWh)")
                .set_TextMatrix(2, 4, "(R$/kW)")
                .set_TextMatrix(2, 5, "(R$/MWh)")
                .set_TextMatrix(2, 6, "(R$/MWh)")
                .set_TextMatrix(2, 7, "(R$/MWh)")
                .set_TextMatrix(2, 8, "(R$/MWh)")
                '
                .set_TextMatrix(3, 0, "RESIDENCIAL")
                .set_TextMatrix(4, 0, "RURAL")
                .set_TextMatrix(5, 0, "RURAL-C.E.R.") '"RURAL - COOPERATIVA DE ELETRIFICAÇÃO RURAL"
                .set_TextMatrix(6, 0, "RURAL-S.P.I.R.") '"RURAL - SERVIÇO PÚBLICO IRRIGAÇÃO RURAL"
                .set_TextMatrix(7, 0, "DEMAIS CLASSES")
                .set_TextMatrix(8, 0, "I.P.-R.DISTRIB.") '"ILUMINAÇÃO PÚBLICA - REDE DE DISTRIBUIÇÃO"
                .set_TextMatrix(9, 0, "I.P.-B.LÂMPADA") '"ILUMINAÇÃO PÚBLICA - BULBO DA LÂMPADA"
                '
                .set_TextMatrix(3, 1, "B1")
                .set_TextMatrix(4, 1, "B2")
                .set_TextMatrix(5, 1, "B2a")
                .set_TextMatrix(6, 1, "B2b")
                .set_TextMatrix(7, 1, "B3")
                .set_TextMatrix(8, 1, "B4a")
                .set_TextMatrix(9, 1, "B4b")
                '
                .set_TextMatrix(0, 9, "BASE ECONÔMICA")
                .set_TextMatrix(0, 10, .get_TextMatrix(0, 9))
                .set_TextMatrix(0, 11, .get_TextMatrix(0, 9))
                .set_TextMatrix(1, 9, "TUSD")
                .set_TextMatrix(1, 10, .get_TextMatrix(1, 9))
                .set_TextMatrix(1, 11, "TE")
                .set_TextMatrix(2, 9, "(R$/kW)")
                .set_TextMatrix(2, 10, "(R$/MWh)")
                .set_TextMatrix(2, 11, "(R$/MWh)")

                .set_TextMatrix(0, 12, "SEM SUBSÍDIO")
                .set_TextMatrix(0, 13, .get_TextMatrix(0, 12))
                .set_TextMatrix(0, 14, .get_TextMatrix(0, 12))
                .set_TextMatrix(1, 12, "TUSD")
                .set_TextMatrix(1, 13, .get_TextMatrix(1, 12))
                .set_TextMatrix(1, 14, "TE")
                .set_TextMatrix(2, 12, "(R$/kW)")
                .set_TextMatrix(2, 13, "(R$/MWh)")
                .set_TextMatrix(2, 14, "(R$/MWh)")
                '
                .set_TextMatrix(0, 15, "FIO - B")
                .set_TextMatrix(0, 16, .get_TextMatrix(0, 15))
                .set_TextMatrix(1, 15, "TUSD")
                .set_TextMatrix(1, 16, .get_TextMatrix(1, 15))
                .set_TextMatrix(2, 15, "(R$/kW)")
                .set_TextMatrix(2, 16, "(R$/MWh)")
                '
                .set_MergeCol(0, True)
                .set_MergeCol(1, True)
                .set_MergeRow(0, True)
                .set_MergeRow(1, True)

                .Row = 3
                .Col = 4
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PrepararFlexGridGrupoBBX()

        Try

            With Me.mfgGrupoBBX
                Call .Clear()
                .Cols = 17
                .Rows = 7
                .FixedRows = 6
                .FixedCols = 4
                .MergeCells = MSFlexGridLib.MergeCellsSettings.flexMergeFree
                .set_ColWidth(0, 1400)
                .set_ColWidth(1, 750)
                .set_ColWidth(2, 1000)
                .set_ColWidth(3, 1000)
                .set_ColWidth(4, 1000)
                .set_ColWidth(5, 1000)
                .set_ColWidth(6, 1000)
                .set_ColWidth(7, 1000)
                .set_ColWidth(8, 1000)
                .set_ColWidth(9, 1000)
                .set_ColWidth(10, 1000)
                .set_ColWidth(11, 1000)
                .set_ColWidth(12, 1000)
                .set_ColWidth(13, 1000)
                .set_ColWidth(14, 1000)
                .set_ColWidth(15, 1000)
                .set_ColWidth(16, 1000)
                'Primeira Linha
                .Row = 0
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 7
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 8
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Segunda Linha
                .Row = 1
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Terceira Linha
                .Row = 2
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                '
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(9, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(10, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(11, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(12, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(13, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(14, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(15, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(16, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                '
                .set_TextMatrix(0, 2, "TUSD+TE")
                .set_TextMatrix(0, 3, .get_TextMatrix(0, 2))
                .set_TextMatrix(0, 4, "TUSD")
                .set_TextMatrix(0, 5, .get_TextMatrix(0, 4))
                .set_TextMatrix(0, 6, "TE")
                .set_TextMatrix(0, 7, .get_TextMatrix(0, 6))
                .set_TextMatrix(0, 8, .get_TextMatrix(0, 6))
                '
                .set_TextMatrix(1, 2, "DEMANDA")
                .set_TextMatrix(1, 3, "ENERGIA")
                .set_TextMatrix(1, 4, "DEMANDA")
                .set_TextMatrix(1, 5, "ENERGIA")
                .set_TextMatrix(1, 6, "BVD")
                .set_TextMatrix(1, 7, "BAM")
                .set_TextMatrix(1, 8, "BVM")
                '
                .set_TextMatrix(0, 0, "SUBCLASSE")
                .set_TextMatrix(1, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(2, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(0, 1, "SUB")
                .set_TextMatrix(1, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 2, "(R$/kW)")
                .set_TextMatrix(2, 3, "(R$/MWh)")
                .set_TextMatrix(2, 4, "(R$/kW)")
                .set_TextMatrix(2, 5, "(R$/MWh)")
                .set_TextMatrix(2, 6, "(R$/MWh)")
                .set_TextMatrix(2, 7, "(R$/MWh)")
                .set_TextMatrix(2, 8, "(R$/MWh)")
                '
                .set_TextMatrix(3, 0, "  <=30")
                .set_TextMatrix(4, 0, "( >30 e <=100 )")
                .set_TextMatrix(5, 0, "( >100 e <=220 )")
                .set_TextMatrix(6, 0, "  >220")
                '
                .set_TextMatrix(3, 1, "65%")
                .set_TextMatrix(4, 1, "40%")
                .set_TextMatrix(5, 1, "10%")
                .set_TextMatrix(6, 1, "00%")
                '
                .set_TextMatrix(0, 9, "BASE ECONÔMICA")
                .set_TextMatrix(0, 10, .get_TextMatrix(0, 9))
                .set_TextMatrix(0, 11, .get_TextMatrix(0, 9))
                .set_TextMatrix(1, 9, "TUSD")
                .set_TextMatrix(1, 10, .get_TextMatrix(1, 9))
                .set_TextMatrix(1, 11, "TE")
                .set_TextMatrix(2, 9, "(R$/kW)")
                .set_TextMatrix(2, 10, "(R$/MWh)")
                .set_TextMatrix(2, 11, "(R$/MWh)")
                '
                .set_TextMatrix(0, 12, "SEM SUBSÍDIO")
                .set_TextMatrix(0, 13, .get_TextMatrix(0, 12))
                .set_TextMatrix(0, 14, .get_TextMatrix(0, 12))
                .set_TextMatrix(1, 12, "TUSD")
                .set_TextMatrix(1, 13, .get_TextMatrix(1, 12))
                .set_TextMatrix(1, 14, "TE")
                .set_TextMatrix(2, 12, "(R$/kW)")
                .set_TextMatrix(2, 13, "(R$/MWh)")
                .set_TextMatrix(2, 14, "(R$/MWh)")
                '
                .set_TextMatrix(0, 15, "FIO - B")
                .set_TextMatrix(0, 16, .get_TextMatrix(0, 15))
                .set_TextMatrix(1, 15, "TUSD")
                .set_TextMatrix(1, 16, .get_TextMatrix(1, 15))
                .set_TextMatrix(2, 15, "(R$/kW)")
                .set_TextMatrix(2, 16, "(R$/MWh)")
                '
                .set_MergeCol(0, True)
                .set_MergeCol(1, True)
                .set_MergeRow(0, True)
                .set_MergeRow(1, True)

                .Row = 6
                .Col = 4

            End With


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PrepararFlexGridGrupoBBR()

        Try

            With Me.mfgGrupoBBR
                Call .Clear()
                .Cols = 17
                .Rows = 18
                .FixedRows = 3
                .FixedCols = 4
                .MergeCells = MSFlexGridLib.MergeCellsSettings.flexMergeFree
                .set_ColWidth(0, 1400)
                .set_ColWidth(1, 750)
                .set_ColWidth(2, 1000)
                .set_ColWidth(3, 1000)
                .set_ColWidth(4, 1000)
                .set_ColWidth(5, 1000)
                .set_ColWidth(6, 1000)
                .set_ColWidth(7, 1000)
                .set_ColWidth(8, 1000)
                .set_ColWidth(9, 1000)
                .set_ColWidth(10, 1000)
                .set_ColWidth(11, 1000)
                .set_ColWidth(12, 1000)
                .set_ColWidth(13, 1000)
                .set_ColWidth(14, 1000)
                .set_ColWidth(15, 1000)
                .set_ColWidth(16, 1000)
                'Primeira Linha
                .Row = 0
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 7
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 8
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Segunda Linha
                .Row = 1
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                'Terceira Linha
                .Row = 2
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 5
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 6
                .CellBackColor = System.Drawing.Color.Lime
                .Col = 7
                .CellBackColor = System.Drawing.Color.Yellow
                .Col = 8
                .CellBackColor = System.Drawing.Color.Red
                .Col = 9
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 10
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 11
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 12
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 13
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 14
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 15
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .Col = 16
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                '
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(9, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(10, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(11, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(12, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(13, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(14, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(15, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(16, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                '
                .set_TextMatrix(0, 2, "TUSD+TE")
                .set_TextMatrix(0, 3, .get_TextMatrix(0, 2))
                .set_TextMatrix(0, 4, "TUSD")
                .set_TextMatrix(0, 5, .get_TextMatrix(0, 4))
                .set_TextMatrix(0, 6, "TE")
                .set_TextMatrix(0, 7, .get_TextMatrix(0, 6))
                .set_TextMatrix(0, 8, .get_TextMatrix(0, 6))
                '
                .set_TextMatrix(1, 2, "DEMANDA")
                .set_TextMatrix(1, 3, "ENERGIA")
                .set_TextMatrix(1, 4, "DEMANDA")
                .set_TextMatrix(1, 5, "ENERGIA")
                .set_TextMatrix(1, 6, "BVD")
                .set_TextMatrix(1, 7, "BAM")
                .set_TextMatrix(1, 8, "BVM")
                '
                .set_TextMatrix(0, 0, "SUBCLASSE")
                .set_TextMatrix(1, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(2, 0, .get_TextMatrix(0, 0))
                .set_TextMatrix(0, 1, "POSTO")
                .set_TextMatrix(1, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 1, .get_TextMatrix(0, 1))
                .set_TextMatrix(2, 2, "(R$/kW)")
                .set_TextMatrix(2, 3, "(R$/MWh)")
                .set_TextMatrix(2, 4, "(R$/kW)")
                .set_TextMatrix(2, 5, "(R$/MWh)")
                .set_TextMatrix(2, 6, "(R$/MWh)")
                .set_TextMatrix(2, 7, "(R$/MWh)")
                .set_TextMatrix(2, 8, "(R$/MWh)")
                '
                .set_TextMatrix(3, 0, "RESIDENCIAL")
                .set_TextMatrix(4, 0, .get_TextMatrix(3, 0))
                .set_TextMatrix(5, 0, .get_TextMatrix(3, 0))
                .set_TextMatrix(6, 0, "RURAL")
                .set_TextMatrix(7, 0, .get_TextMatrix(6, 0))
                .set_TextMatrix(8, 0, .get_TextMatrix(6, 0))
                .set_TextMatrix(9, 0, "RURAL-C.E.R.")
                .set_TextMatrix(10, 0, .get_TextMatrix(9, 0))
                .set_TextMatrix(11, 0, .get_TextMatrix(9, 0))
                .set_TextMatrix(12, 0, "RURAL-S.P.I.R.")
                .set_TextMatrix(13, 0, .get_TextMatrix(12, 0))
                .set_TextMatrix(14, 0, .get_TextMatrix(12, 0))
                .set_TextMatrix(15, 0, "DEMAIS CLASSES")
                .set_TextMatrix(16, 0, .get_TextMatrix(15, 0))
                .set_TextMatrix(17, 0, .get_TextMatrix(15, 0))
                '
                .set_TextMatrix(3, 1, "P")
                .set_TextMatrix(4, 1, "INT")
                .set_TextMatrix(5, 1, "FP")
                .set_TextMatrix(6, 1, .get_TextMatrix(3, 1))
                .set_TextMatrix(7, 1, .get_TextMatrix(4, 1))
                .set_TextMatrix(8, 1, .get_TextMatrix(5, 1))
                .set_TextMatrix(9, 1, .get_TextMatrix(3, 1))
                .set_TextMatrix(10, 1, .get_TextMatrix(4, 1))
                .set_TextMatrix(11, 1, .get_TextMatrix(5, 1))
                .set_TextMatrix(12, 1, .get_TextMatrix(3, 1))
                .set_TextMatrix(13, 1, .get_TextMatrix(4, 1))
                .set_TextMatrix(14, 1, .get_TextMatrix(5, 1))
                .set_TextMatrix(15, 1, .get_TextMatrix(3, 1))
                .set_TextMatrix(16, 1, .get_TextMatrix(4, 1))
                .set_TextMatrix(17, 1, .get_TextMatrix(5, 1))
                '
                .set_TextMatrix(0, 9, "BASE ECONÔMICA")
                .set_TextMatrix(0, 10, .get_TextMatrix(0, 9))
                .set_TextMatrix(0, 11, .get_TextMatrix(0, 9))
                .set_TextMatrix(1, 9, "TUSD")
                .set_TextMatrix(1, 10, .get_TextMatrix(1, 9))
                .set_TextMatrix(1, 11, "TE")
                .set_TextMatrix(2, 9, "(R$/kW)")
                .set_TextMatrix(2, 10, "(R$/MWh)")
                .set_TextMatrix(2, 11, "(R$/MWh)")

                .set_TextMatrix(0, 12, "SEM SUBSÍDIO")
                .set_TextMatrix(0, 13, .get_TextMatrix(0, 12))
                .set_TextMatrix(0, 14, .get_TextMatrix(0, 12))
                .set_TextMatrix(1, 12, "TUSD")
                .set_TextMatrix(1, 13, .get_TextMatrix(1, 12))
                .set_TextMatrix(1, 14, "TE")
                .set_TextMatrix(2, 12, "(R$/kW)")
                .set_TextMatrix(2, 13, "(R$/MWh)")
                .set_TextMatrix(2, 14, "(R$/MWh)")
                '
                .set_TextMatrix(0, 15, "FIO - B")
                .set_TextMatrix(0, 16, .get_TextMatrix(0, 15))
                .set_TextMatrix(1, 15, "TUSD")
                .set_TextMatrix(1, 16, .get_TextMatrix(1, 15))
                .set_TextMatrix(2, 15, "(R$/kW)")
                .set_TextMatrix(2, 16, "(R$/MWh)")
                '
                .set_MergeCol(0, True)
                .set_MergeCol(1, True)
                .set_MergeRow(0, True)
                .set_MergeRow(1, True)

                .Row = 3
                .Col = 4
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PreencheCombobox()

        Try

            PreencherTarifa_1(cmbData)

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Function VerificaSeExiste(Optional ByVal astrCod As String = "") As Boolean

        Dim lblnRet As Boolean

        Dim objTarifa As New entidadeTarifa
        Dim repositorioTarifa As New repositorioTarifa

        Dim dt As DataTable

        Try

            If IsDate(Me.cmbData.Text) Then

                objTarifa.NUM = astrCod
                objTarifa.REFERENCIA = Convert.ToDateTime(Format(Convert.ToDateTime(Me.cmbData.Text), "dd/MM/yyyy"))

                dt = repositorioTarifa.SelectCount(objTarifa)

                If Convert.ToInt32(dt.Rows(0).ItemArray(0).ToString()) > 0 Then
                    lblnRet = True
                Else
                    lblnRet = False
                End If

            End If

            VerificaSeExiste = lblnRet

        Catch ex As Exception
            Exit Function
        End Try

    End Function

    Private Sub LigDes(ByVal ablnModo As Boolean)


        Try

            Me.cmdLocalizar.Enabled = ablnModo
            Me.cmdSalvar.Enabled = ablnModo
            Me.cmdImprimir.Enabled = ablnModo
            Me.cmdFechar.Enabled = ablnModo
            Me.cmbData.Enabled = ablnModo
            Me.sstTarifas.Enabled = ablnModo
            Me.sstSubTarifas.Enabled = ablnModo
            Me.cmdExcluir.Enabled = ablnModo
            Me.cmdComposicao.Enabled = ablnModo

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub SetaVariaveis()

        Dim llngAux As Integer

        Try

            'Grupo A
            With Me.mfgGrupoACodigos
                Call .Clear()
                .Cols = 12
                .Rows = 18
                '
                'Tratar LIVRE pois não tem TE somente TUSD - Usar linha 3, 4 do AZUL
                '
                'linha 3 e 4 AZUL
                'Ponta
                .set_TextMatrix(3, 0, "01AA01")
                .set_TextMatrix(3, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(3, 3, "1101;1105") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(3, 4, "2101;2103") 'TUSD (DEMANDA);Ultrapassagem
                .set_TextMatrix(3, 5, "1201;1205") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(3, 6, "1301;1305") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(3, 7, "valor_amarela")
                .set_TextMatrix(3, 8, "valor_vermelha")
                .set_TextMatrix(3, 9, "valor_base_economica")
                .set_TextMatrix(3, 10, "valor_sem_subsidio")
                .set_TextMatrix(3, 11, "valor_tusd_fio_b")
                '
                'Fora de Ponta
                .set_TextMatrix(4, 0, "01AA01")
                .set_TextMatrix(4, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(4, 3, "1102;1106") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(4, 4, "2102;2104") 'TUSD (DEMANDA);Ultrapassagem Fora de Ponta
                .set_TextMatrix(4, 5, "1202;1206") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(4, 6, "1302;1306") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(4, 7, "valor_amarela")
                .set_TextMatrix(4, 8, "valor_vermelha")
                .set_TextMatrix(4, 9, "valor_base_economica")
                .set_TextMatrix(4, 10, "valor_sem_subsidio")
                .set_TextMatrix(4, 11, "valor_tusd_fio_b")
                '
                'linha 5 e 6 AZUL APE
                'Ponta
                .set_TextMatrix(5, 0, "01AP01")
                .set_TextMatrix(5, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(5, 3, "1101;1105") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(5, 4, "2101;2103") 'TUSD (DEMANDA);Ultrapassagem
                .set_TextMatrix(5, 5, "1201;1205") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(5, 6, "1301;1305") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(5, 7, "valor_amarela")
                .set_TextMatrix(5, 8, "valor_vermelha")
                .set_TextMatrix(5, 9, "valor_base_economica")
                .set_TextMatrix(5, 10, "valor_sem_subsidio")
                .set_TextMatrix(5, 11, "valor_tusd_fio_b")
                '
                'Fora de Ponta
                .set_TextMatrix(6, 0, "01AP01")
                .set_TextMatrix(6, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(6, 3, "1102;1106") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(6, 4, "2102;2104") 'TUSD (DEMANDA);Ultrapassagem
                .set_TextMatrix(6, 5, "1202;1206") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(6, 6, "1302;1306") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(6, 7, "valor_amarela")
                .set_TextMatrix(6, 8, "valor_vermelha")
                .set_TextMatrix(6, 9, "valor_base_economica")
                .set_TextMatrix(6, 10, "valor_sem_subsidio")
                .set_TextMatrix(6, 11, "valor_tusd_fio_b")
                '
                'linha 7 (NA),8 (P) e 9 (FP) VERDE
                'Não se aplica
                .set_TextMatrix(7, 0, "01AV01")
                .set_TextMatrix(7, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(7, 3, "") 'TUSD + TE Ponta Seca;Úmida (ENERGIA)
                .set_TextMatrix(7, 4, "2102;2104") 'TUSD (DEMANDA);Ultrapassagem
                .set_TextMatrix(7, 5, "") 'TUSD Ponta Seca;Úmida (ENERGIA)
                .set_TextMatrix(7, 6, "") 'TE Ponta Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(7, 7, "valor_amarela")
                .set_TextMatrix(7, 8, "valor_vermelha")
                .set_TextMatrix(7, 9, "valor_base_economica")
                .set_TextMatrix(7, 10, "valor_sem_subsidio")
                .set_TextMatrix(7, 11, "valor_tusd_fio_b")
                '
                'Ponta
                .set_TextMatrix(8, 0, "01AV01")
                .set_TextMatrix(8, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(8, 3, "1101;1105") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(8, 4, "")
                .set_TextMatrix(8, 5, "1201;1205") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(8, 6, "1301;1305") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(8, 7, "valor_amarela")
                .set_TextMatrix(8, 8, "valor_vermelha")
                .set_TextMatrix(8, 9, "valor_base_economica")
                .set_TextMatrix(8, 10, "valor_sem_subsidio")
                .set_TextMatrix(8, 11, "valor_tusd_fio_b")
                '
                'Fora de Ponta
                .set_TextMatrix(9, 0, "01AV01")
                .set_TextMatrix(9, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(9, 3, "1102;1106") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(9, 4, "")
                .set_TextMatrix(9, 5, "1202;1206") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(9, 6, "1302;1306") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(9, 7, "valor_amarela")
                .set_TextMatrix(9, 8, "valor_vermelha")
                .set_TextMatrix(9, 9, "valor_base_economica")
                .set_TextMatrix(9, 10, "valor_sem_subsidio")
                .set_TextMatrix(9, 11, "valor_tusd_fio_b")
                '
                'linha 10 (NA),11 (P) e 12 (FP) VERDE APE
                'Não se aplica
                .set_TextMatrix(10, 0, "01AP02")
                .set_TextMatrix(10, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(10, 3, "") 'TUSD + TE Ponta Seca;Úmida (ENERGIA)
                .set_TextMatrix(10, 4, "2102;2104") 'TUSD (DEMANDA);Ultrapassagem
                .set_TextMatrix(10, 5, "") 'TUSD Ponta Seca;Úmida (ENERGIA)
                .set_TextMatrix(10, 6, "") 'TE Ponta Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(10, 7, "valor_amarela")
                .set_TextMatrix(10, 8, "valor_vermelha")
                .set_TextMatrix(10, 9, "valor_base_economica")
                .set_TextMatrix(10, 10, "valor_sem_subsidio")
                .set_TextMatrix(10, 11, "valor_tusd_fio_b")
                '
                'Ponta
                .set_TextMatrix(11, 0, "01AP02")
                .set_TextMatrix(11, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(11, 3, "1101;1105") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(11, 4, "")
                .set_TextMatrix(11, 5, "1201;1205") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(11, 6, "1301;1305") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(11, 7, "valor_amarela")
                .set_TextMatrix(11, 8, "valor_vermelha")
                .set_TextMatrix(11, 9, "valor_base_economica")
                .set_TextMatrix(11, 10, "valor_sem_subsidio")
                .set_TextMatrix(11, 11, "valor_tusd_fio_b")
                '
                'Fora de Ponta
                .set_TextMatrix(12, 0, "01AP02")
                .set_TextMatrix(12, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(12, 3, "1102;1106") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(12, 4, "")
                .set_TextMatrix(12, 5, "1202;1206") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(12, 6, "1302;1306") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(12, 7, "valor_amarela")
                .set_TextMatrix(12, 8, "valor_vermelha")
                .set_TextMatrix(12, 9, "valor_base_economica")
                .set_TextMatrix(12, 10, "valor_sem_subsidio")
                .set_TextMatrix(12, 11, "valor_tusd_fio_b")
                '
                'linha 13 Convencional
                .set_TextMatrix(13, 0, "01A401")
                .set_TextMatrix(13, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(13, 3, "1101") 'TUSD + TE (ENERGIA)
                .set_TextMatrix(13, 4, "2101;2102") 'TUSD (DEMANDA) 'Ultrapassagem
                .set_TextMatrix(13, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(13, 6, "1301") 'TE (ENERGIA) 'valor
                .set_TextMatrix(13, 7, "valor_amarela")
                .set_TextMatrix(13, 8, "valor_vermelha")
                .set_TextMatrix(13, 9, "valor_base_economica")
                .set_TextMatrix(13, 10, "valor_sem_subsidio")
                .set_TextMatrix(13, 11, "valor_tusd_fio_b")
                '
                'linha 14 Geradora - MUST
                .set_TextMatrix(14, 0, "01BU01")
                .set_TextMatrix(14, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(14, 3, "") 'TUSD + TE (ENERGIA)
                .set_TextMatrix(14, 4, "1401") 'TUSD (DEMANDA)
                .set_TextMatrix(14, 5, "") 'TUSD (ENERGIA)
                .set_TextMatrix(14, 6, "") 'TE (ENERGIA) 'valor verde
                .set_TextMatrix(14, 7, "valor_amarela")
                .set_TextMatrix(14, 8, "valor_vermelha")
                .set_TextMatrix(14, 9, "valor_base_economica")
                .set_TextMatrix(14, 10, "valor_sem_subsidio")
                .set_TextMatrix(14, 11, "valor_tusd_fio_b")
                '
                'linha 15 Supridora
                .set_TextMatrix(15, 0, "01AS01")
                .set_TextMatrix(15, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(15, 3, "1101") 'TUSD + TE (ENERGIA)
                .set_TextMatrix(15, 4, "2101;2102") 'TUSD (DEMANDA) 'Ultrapassagem
                .set_TextMatrix(15, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(15, 6, "1301") 'TE (ENERGIA) 'valor verde
                .set_TextMatrix(15, 7, "valor_amarela")
                .set_TextMatrix(15, 8, "valor_vermelha")
                .set_TextMatrix(15, 9, "valor_base_economica")
                .set_TextMatrix(15, 10, "valor_sem_subsidio")
                .set_TextMatrix(15, 11, "valor_tusd_fio_b")

                ' HORO SUPRIMENTO PONTA
                .set_TextMatrix(16, 0, "01AX01")
                .set_TextMatrix(16, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(16, 3, "1101;1105") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(16, 4, "2101;2103") 'TUSD (DEMANDA);Ultrapassagem
                .set_TextMatrix(16, 5, "1201;1205") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(16, 6, "1301;1305") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(16, 7, "valor_amarela")
                .set_TextMatrix(16, 8, "valor_vermelha")
                .set_TextMatrix(16, 9, "valor_base_economica")
                .set_TextMatrix(16, 10, "valor_sem_subsidio")
                .set_TextMatrix(16, 11, "valor_tusd_fio_b")
                '
                'HORO SUPRIMENTO Fora de Ponta
                .set_TextMatrix(17, 0, "01AX01")
                .set_TextMatrix(17, 2, "") 'TUSD + TE (DEMANDA)
                .set_TextMatrix(17, 3, "1102;1106") 'TUSD + TE Seca;Úmida (ENERGIA)
                .set_TextMatrix(17, 4, "2102;2104") 'TUSD (DEMANDA);Ultrapassagem Fora de Ponta
                .set_TextMatrix(17, 5, "1202;1206") 'TUSD Seca;Úmida (ENERGIA)
                .set_TextMatrix(17, 6, "1302;1306") 'TE Seca;Úmida (ENERGIA) 'valor
                .set_TextMatrix(17, 7, "valor_amarela")
                .set_TextMatrix(17, 8, "valor_vermelha")
                .set_TextMatrix(17, 9, "valor_base_economica")
                .set_TextMatrix(17, 10, "valor_sem_subsidio")
                .set_TextMatrix(17, 11, "valor_tusd_fio_b")
            End With
            '
            'Grupo B Convencional
            With Me.mfgGrupoBCNCodigos
                Call .Clear()
                .Cols = 12
                .Rows = 16
                'Residencial
                .set_TextMatrix(3, 0, "01B101")
                .set_TextMatrix(3, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(3, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(3, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(3, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(3, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(3, 7, "valor_amarela")
                .set_TextMatrix(3, 8, "valor_vermelha")
                .set_TextMatrix(3, 9, "valor_base_economica")
                .set_TextMatrix(3, 10, "valor_sem_subsidio")
                .set_TextMatrix(3, 11, "valor_tusd_fio_b")
                '
                'Rural - Diversos
                .set_TextMatrix(4, 0, "01B201")
                .set_TextMatrix(4, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(4, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(4, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(4, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(4, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(4, 7, "valor_amarela")
                .set_TextMatrix(4, 8, "valor_vermelha")
                .set_TextMatrix(4, 9, "valor_base_economica")
                .set_TextMatrix(4, 10, "valor_sem_subsidio")
                .set_TextMatrix(4, 11, "valor_tusd_fio_b")
                'Rural - Cooperativa
                .set_TextMatrix(5, 0, "01B202")
                .set_TextMatrix(5, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(5, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(5, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(5, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(5, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(5, 7, "valor_amarela")
                .set_TextMatrix(5, 8, "valor_vermelha")
                .set_TextMatrix(5, 9, "valor_base_economica")
                .set_TextMatrix(5, 10, "valor_sem_subsidio")
                .set_TextMatrix(5, 11, "valor_tusd_fio_b")
                'Rural - Serviço de Irrigação
                .set_TextMatrix(6, 0, "01B203")
                .set_TextMatrix(6, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(6, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(6, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(6, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(6, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(6, 7, "valor_amarela")
                .set_TextMatrix(6, 8, "valor_vermelha")
                .set_TextMatrix(6, 9, "valor_base_economica")
                .set_TextMatrix(6, 10, "valor_sem_subsidio")
                .set_TextMatrix(6, 11, "valor_tusd_fio_b")
                '
                'Industrial 10 'Serviço Público 20 'Comercial 30 'Consumo Próprio 40 'Poderes Públicos 50
                .set_TextMatrix(7, 0, "01B310")
                .set_TextMatrix(7, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(7, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(7, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(7, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(7, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(7, 7, "valor_amarela")
                .set_TextMatrix(7, 8, "valor_vermelha")
                .set_TextMatrix(7, 9, "valor_base_economica")
                .set_TextMatrix(7, 10, "valor_sem_subsidio")
                .set_TextMatrix(7, 11, "valor_tusd_fio_b")
                '
                'IP - Rede
                .set_TextMatrix(8, 0, "01B401")
                .set_TextMatrix(8, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(8, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(8, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(8, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(8, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(8, 7, "valor_amarela")
                .set_TextMatrix(8, 8, "valor_vermelha")
                .set_TextMatrix(8, 9, "valor_base_economica")
                .set_TextMatrix(8, 10, "valor_sem_subsidio")
                .set_TextMatrix(8, 11, "valor_tusd_fio_b")
                'IP - Bulbo
                .set_TextMatrix(9, 0, "01B402")
                .set_TextMatrix(9, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(9, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(9, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(9, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(9, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(9, 7, "valor_amarela")
                .set_TextMatrix(9, 8, "valor_vermelha")
                .set_TextMatrix(9, 9, "valor_base_economica")
                .set_TextMatrix(9, 10, "valor_sem_subsidio")
                .set_TextMatrix(9, 11, "valor_tusd_fio_b")
            End With
            '
            'Baixa Renda
            With Me.mfgGrupoBBXCodigos
                'até 30 kwh
                Call .Clear()
                .Cols = 12
                .Rows = 7
                .set_TextMatrix(3, 0, "01B102")
                .set_TextMatrix(3, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(3, 3, "1101") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(3, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(3, 5, "1201") 'TUSD (ENERGIA)
                .set_TextMatrix(3, 6, "1301") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(3, 7, "valor_amarela")
                .set_TextMatrix(3, 8, "valor_vermelha")
                .set_TextMatrix(3, 9, "valor_base_economica")
                .set_TextMatrix(3, 10, "valor_sem_subsidio")
                .set_TextMatrix(3, 11, "valor_tusd_fio_b")
                '31-100 kwh
                .set_TextMatrix(4, 0, "01B102")
                .set_TextMatrix(4, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(4, 3, "1102") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(4, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(4, 5, "1202") 'TUSD (ENERGIA)
                .set_TextMatrix(4, 6, "1302") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(4, 7, "valor_amarela")
                .set_TextMatrix(4, 8, "valor_vermelha")
                .set_TextMatrix(4, 9, "valor_base_economica")
                .set_TextMatrix(4, 10, "valor_sem_subsidio")
                .set_TextMatrix(4, 11, "valor_tusd_fio_b")
                '101-220 kwh
                .set_TextMatrix(5, 0, "01B102")
                .set_TextMatrix(5, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(5, 3, "1103") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(5, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(5, 5, "1203") 'TUSD (ENERGIA)
                .set_TextMatrix(5, 6, "1303") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(5, 7, "valor_amarela")
                .set_TextMatrix(5, 8, "valor_vermelha")
                .set_TextMatrix(5, 9, "valor_base_economica")
                .set_TextMatrix(5, 10, "valor_sem_subsidio")
                .set_TextMatrix(5, 11, "valor_tusd_fio_b")
                '220 kWh acima
                .set_TextMatrix(6, 0, "01B102")
                .set_TextMatrix(6, 2, "") 'TUSD + TE(DEMANDA)
                .set_TextMatrix(6, 3, "1104") 'TUSD + TE(ENETRGIA)
                .set_TextMatrix(6, 4, "") 'TUSD (DEMANDA)
                .set_TextMatrix(6, 5, "1204") 'TUSD (ENERGIA)
                .set_TextMatrix(6, 6, "1304") 'TE (ENERGIA) ' valor verde
                .set_TextMatrix(6, 7, "valor_amarela")
                .set_TextMatrix(6, 8, "valor_vermelha")
                .set_TextMatrix(6, 9, "valor_base_economica")
                .set_TextMatrix(6, 10, "valor_sem_subsidio")
                .set_TextMatrix(6, 11, "valor_tusd_fio_b")
            End With
            'Branca
            With Me.mfgGrupoBBRCodigos
                Call .Clear()
                .Cols = 12
                .Rows = 18
                For llngAux = 3 To .Rows - 1 Step 3
                    'Ponta
                    If llngAux = 3 Then
                        .set_TextMatrix(llngAux, 0, "01B191") 'RESIDENCIAL BRANCA
                    ElseIf llngAux = 6 Then
                        .set_TextMatrix(llngAux, 0, "01B291") 'RURAL BRANCA
                    ElseIf llngAux = 9 Then
                        .set_TextMatrix(llngAux, 0, "01B292") 'COOP.ELETR.RURAL BRANCA
                    ElseIf llngAux = 12 Then
                        .set_TextMatrix(llngAux, 0, "01B293") 'SERV.PUB.IRRIG. BRANCA
                    ElseIf llngAux = 15 Then
                        .set_TextMatrix(llngAux, 0, "01B391") 'DEMAIS CLASSES BRANCA
                    End If
                    .set_TextMatrix(llngAux, 2, "") 'TUSD + TE(DEMANDA)
                    .set_TextMatrix(llngAux, 3, "1101") 'TUSD + TE(ENETRGIA)
                    .set_TextMatrix(llngAux, 4, "") 'TUSD (DEMANDA)
                    .set_TextMatrix(llngAux, 5, "1201") 'TUSD (ENERGIA)
                    .set_TextMatrix(llngAux, 6, "1301") 'TE (ENERGIA) ' valor verde
                    .set_TextMatrix(llngAux, 7, "valor_amarela")
                    .set_TextMatrix(llngAux, 8, "valor_vermelha")
                    .set_TextMatrix(llngAux, 9, "valor_base_economica")
                    .set_TextMatrix(llngAux, 10, "valor_sem_subsidio")
                    .set_TextMatrix(llngAux, 11, "valor_tusd_fio_b")
                    'Intermediário
                    .set_TextMatrix(llngAux + 1, 0, .get_TextMatrix(llngAux, 0))
                    .set_TextMatrix(llngAux + 1, 2, "") 'TUSD + TE(DEMANDA)
                    .set_TextMatrix(llngAux + 1, 3, "1102") 'TUSD + TE(ENETRGIA)
                    .set_TextMatrix(llngAux + 1, 4, "") 'TUSD (DEMANDA)
                    .set_TextMatrix(llngAux + 1, 5, "1202") 'TUSD (ENERGIA)
                    .set_TextMatrix(llngAux + 1, 6, "1302") 'TE (ENERGIA) ' valor verde
                    .set_TextMatrix(llngAux + 1, 7, "valor_amarela")
                    .set_TextMatrix(llngAux + 1, 8, "valor_vermelha")
                    .set_TextMatrix(llngAux + 1, 9, "valor_base_economica")
                    .set_TextMatrix(llngAux + 1, 10, "valor_sem_subsidio")
                    .set_TextMatrix(llngAux + 1, 11, "valor_tusd_fio_b")
                    'Fora de Ponta
                    .set_TextMatrix(llngAux + 2, 0, .get_TextMatrix(llngAux, 0))
                    .set_TextMatrix(llngAux + 2, 2, "") 'TUSD + TE(DEMANDA)
                    .set_TextMatrix(llngAux + 2, 3, "1103") 'TUSD + TE(ENETRGIA)
                    .set_TextMatrix(llngAux + 2, 4, "") 'TUSD (DEMANDA)
                    .set_TextMatrix(llngAux + 2, 5, "1203") 'TUSD (ENERGIA)
                    .set_TextMatrix(llngAux + 2, 6, "1303") 'TE (ENERGIA) ' valor verde
                    .set_TextMatrix(llngAux + 2, 7, "valor_amarela")
                    .set_TextMatrix(llngAux + 2, 8, "valor_vermelha")
                    .set_TextMatrix(llngAux + 2, 9, "valor_base_economica")
                    .set_TextMatrix(llngAux + 2, 10, "valor_sem_subsidio")
                    .set_TextMatrix(llngAux + 2, 11, "valor_tusd_fio_b")
                Next llngAux
            End With


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PegaRowColGrade(ByRef amfgAux As AxMSFlexGridLib.AxMSFlexGrid, ByVal astrCod As String, ByRef alngCol As Integer, ByRef alngRow As Integer, ByVal astrPai As String)

        Dim llngRow As Integer
        Dim llngCol As Integer

        Try

            With amfgAux
                For llngRow = 0 To .Rows - 1
                    If Microsoft.VisualBasic.Left(.get_TextMatrix(llngRow, 0), CShort(Len(astrPai))) = astrPai Then
                        alngRow = llngRow
                        For llngCol = 2 To .Cols - 1
                            If Microsoft.VisualBasic.Left(.get_TextMatrix(llngRow, llngCol), 4) = Microsoft.VisualBasic.Right(astrCod, 4) Then
                                alngCol = llngCol
                                llngCol = .Cols - 1
                                llngRow = .Rows - 1
                            End If
                        Next llngCol
                    End If
                Next llngRow
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub TotalizaValorGrade(ByRef mfgAux As AxMSFlexGridLib.AxMSFlexGrid, ByVal alngRow As Integer)

        Dim llngCol As Integer

        Try

            With mfgAux
                If .Name = "mfgGrupoBBX" Then
                    If alngRow = 6 Then
                        For llngCol = 4 To 16
                            .set_TextMatrix(alngRow - 1, llngCol, Format(Arredondar(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, llngCol)), .get_TextMatrix(alngRow, llngCol), 0)) - (Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, llngCol)), .get_TextMatrix(alngRow, llngCol), 0)) * (Convert.ToDouble(IIf(IsNumeric(Replace(.get_TextMatrix(alngRow - 1, 1), "%", "")), Replace(.get_TextMatrix(alngRow - 1, 1), "%", ""), 0)) / 100)), 2), "#,##0.00"))
                            .set_TextMatrix(alngRow - 2, llngCol, Format(Arredondar(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, llngCol)), .get_TextMatrix(alngRow, llngCol), 0)) - (Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, llngCol)), .get_TextMatrix(alngRow, llngCol), 0)) * (Convert.ToDouble(IIf(IsNumeric(Replace(.get_TextMatrix(alngRow - 2, 1), "%", "")), Replace(.get_TextMatrix(alngRow - 2, 1), "%", ""), 0)) / 100)), 2), "#,##0.00"))
                            .set_TextMatrix(alngRow - 3, llngCol, Format(Arredondar(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, llngCol)), .get_TextMatrix(alngRow, llngCol), 0)) - (Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, llngCol)), .get_TextMatrix(alngRow, llngCol), 0)) * (Convert.ToDouble(IIf(IsNumeric(Replace(.get_TextMatrix(alngRow - 3, 1), "%", "")), Replace(.get_TextMatrix(alngRow - 3, 1), "%", ""), 0)) / 100)), 2), "#,##0.00"))
                        Next llngCol
                    End If
                End If
                .set_TextMatrix(alngRow, 2, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, 4)), .get_TextMatrix(alngRow, 4), 0)), "#,##0.00"))
                .set_TextMatrix(alngRow, 3, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, 5)), .get_TextMatrix(alngRow, 5), 0)) + Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow, 6)), .get_TextMatrix(alngRow, 6), 0)), "#,##0.00"))
                If .Name = "mfgGrupoBBX" Then
                    If alngRow = 6 Then
                        .set_TextMatrix(alngRow - 1, 2, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 1, 4)), .get_TextMatrix(alngRow - 1, 4), 0)), "#,##0.00"))
                        .set_TextMatrix(alngRow - 1, 3, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 1, 5)), .get_TextMatrix(alngRow - 1, 5), 0)) + Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 1, 6)), .get_TextMatrix(alngRow - 1, 6), 0)), "#,##0.00"))
                        .set_TextMatrix(alngRow - 2, 2, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 2, 4)), .get_TextMatrix(alngRow - 2, 4), 0)), "#,##0.00"))
                        .set_TextMatrix(alngRow - 2, 3, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 2, 5)), .get_TextMatrix(alngRow - 2, 5), 0)) + Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 2, 6)), .get_TextMatrix(alngRow - 2, 6), 0)), "#,##0.00"))
                        .set_TextMatrix(alngRow - 3, 2, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 3, 4)), .get_TextMatrix(alngRow - 3, 4), 0)), "#,##0.00"))
                        .set_TextMatrix(alngRow - 3, 3, Format(Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 3, 5)), .get_TextMatrix(alngRow - 3, 5), 0)) + Convert.ToDouble(IIf(IsNumeric(.get_TextMatrix(alngRow - 3, 6)), .get_TextMatrix(alngRow - 3, 6), 0)), "#,##0.00"))
                    End If
                End If
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub AtualizaValorGrade(ByRef mfgAux As AxMSFlexGridLib.AxMSFlexGrid, ByRef txtAux As System.Windows.Forms.TextBox)

        Try

            With mfgAux
                .set_TextMatrix(.Row, .Col, Format(Convert.ToDouble(IIf(IsNumeric(txtAux.Text), txtAux.Text, 0)), "#,##0.00"))
                If .Col = 6 Then
                    If IIf(IsNumeric(txtAux.Text), txtAux.Text, 0) > 0 Then
                        .set_TextMatrix(.Row, .Col + 1, Format(Convert.ToDouble(IIf(IsNumeric(txtAux.Text), txtAux.Text, 0)) + 25, "#,##0.00"))
                        .set_TextMatrix(.Row, .Col + 2, Format(Convert.ToDouble(IIf(IsNumeric(txtAux.Text), txtAux.Text, 0)) + 55, "#,##0.00"))
                    Else
                        .set_TextMatrix(.Row, .Col + 1, Format(Convert.ToDouble(0), "#,##0.00"))
                        .set_TextMatrix(.Row, .Col + 2, Format(Convert.ToDouble(0), "#,##0.00"))
                    End If
                End If
                TotalizaValorGrade(mfgAux, .Row)

            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub MontaTarifa(ByRef mfgAuxCodigos As AxMSFlexGridLib.AxMSFlexGrid, ByRef mfgAuxDados As AxMSFlexGridLib.AxMSFlexGrid)

        Dim llngRow As Integer
        Dim llngCol As Integer
        Dim lstrPre As String
        Dim lintAux As Short

        Dim lstrCod(7, 2) As String
        Dim ldblVlr(10, 2) As Double

        Try

            With mfgAuxCodigos
                For llngRow = 3 To .Rows - 1
                    lstrPre = .get_TextMatrix(llngRow, 0)

                    For llngCol = 2 To 6
                        lstrCod(llngCol - 1, 1) = IIf(CShort(Len(Trim(.get_TextMatrix(llngRow, llngCol)))) > 0, lstrPre & Mid(.get_TextMatrix(llngRow, llngCol), 1, 4), "")
                        lstrCod(llngCol - 1, 2) = IIf(CShort(InStr(1, .get_TextMatrix(llngRow, llngCol), ";")) > 0, lstrPre & Mid(.get_TextMatrix(llngRow, llngCol), 6, 4), "")
                        If CShort(Len(lstrCod(llngCol - 1, 1))) > 0 Then
                            ldblVlr(llngCol - 1, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol)), mfgAuxDados.get_TextMatrix(llngRow, llngCol), 0))
                            ldblVlr(8, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 5)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 5), 0)) 'BE
                            ldblVlr(9, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 8)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 8), 0)) 'Sem subsidio
                            'If Strings.Mid(lstrCod(llngCol - 1, 1), 6, 2) = "12" Then
                            'If llngCol = 3 Then
                            If llngCol < 6 Then
                                ldblVlr(10, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 11)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 11), 0)) 'Fio B
                            Else
                                ldblVlr(10, 1) = 0
                            End If
                            'If llngCol = 4 Then
                            'ldblVlr(10, 2) = Conversion.CDbl(IIf(IsNumeric(mfgAuxDados.TextMatrix(llngRow, 16)), mfgAuxDados.TextMatrix(llngRow, 16), 0))         'Fio B
                            'End If
                        Else
                            ldblVlr(llngCol - 1, 1) = 0
                            ldblVlr(8, 1) = 0
                            ldblVlr(9, 1) = 0
                            ldblVlr(10, 2) = 0
                        End If
                        If CShort(Len(lstrCod(llngCol - 1, 2))) > 0 Then
                            If CShort(Mid(lstrCod(llngCol - 1, 2), 7, 2)) = 21 Then
                                ldblVlr(llngCol - 1, 2) = (ldblVlr(llngCol - 1, 1) * 2)
                                ldblVlr(8, 2) = (ldblVlr(8, 1) * 2)
                                ldblVlr(9, 2) = (ldblVlr(9, 1) * 2)
                                ldblVlr(10, 2) = (ldblVlr(10, 1) * 2)
                            Else
                                ldblVlr(llngCol - 1, 2) = ldblVlr(llngCol - 1, 1)
                                ldblVlr(8, 2) = ldblVlr(8, 1)
                                ldblVlr(9, 2) = ldblVlr(9, 1)
                                ldblVlr(10, 2) = ldblVlr(10, 1)
                            End If
                        Else
                            ldblVlr(llngCol - 1, 2) = 0
                            ldblVlr(8, 2) = 0
                            ldblVlr(9, 2) = 0
                            ldblVlr(10, 2) = 0
                        End If
                        ' If CDbl(Mid(lstrCod(llngCol - 1, 1), 7, 2)) = 13 Then
                        If Mid(lstrCod(llngCol - 1, 1), 7, 2) = "13" Then
                            ldblVlr(6, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, 7)), mfgAuxDados.get_TextMatrix(llngRow, 7), 0))
                            ldblVlr(6, 2) = 0
                            ldblVlr(7, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, 8)), mfgAuxDados.get_TextMatrix(llngRow, 8), 0))
                            ldblVlr(7, 2) = 0
                        ElseIf Mid(lstrCod(llngCol - 1, 1), 7, 2) = "11" Then
                            ' ElseIf CDbl(Mid(lstrCod(llngCol - 1, 1), 7, 2)) = 11 Then
                            ldblVlr(6, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, 7)), mfgAuxDados.get_TextMatrix(llngRow, 7), 0))
                            If ldblVlr(6, 1) > 0 Then
                                ldblVlr(6, 1) = ldblVlr(6, 1) + CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, 5)), mfgAuxDados.get_TextMatrix(llngRow, 5), 0))
                            End If
                            ldblVlr(6, 2) = ldblVlr(6, 1)
                            '
                            ldblVlr(7, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, 8)), mfgAuxDados.get_TextMatrix(llngRow, 8), 0))
                            If ldblVlr(7, 1) > 0 Then
                                ldblVlr(7, 1) = ldblVlr(7, 1) + CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, 5)), mfgAuxDados.get_TextMatrix(llngRow, 5), 0))
                            End If
                            ldblVlr(7, 2) = ldblVlr(7, 1)
                            '
                            ldblVlr(8, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 7)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 7), 0))
                            If ldblVlr(8, 1) > 0 Then
                                ldblVlr(8, 1) = ldblVlr(8, 1) + CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 8)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 8), 0))
                            End If
                            ldblVlr(8, 2) = ldblVlr(8, 1)

                            ldblVlr(9, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 10)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 10), 0))
                            If ldblVlr(9, 1) > 0 Then
                                ldblVlr(9, 1) = ldblVlr(9, 1) + CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 11)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 11), 0))
                            End If
                            ldblVlr(9, 2) = ldblVlr(9, 1)

                            ldblVlr(10, 1) = CDbl(IIf(IsNumeric(mfgAuxDados.get_TextMatrix(llngRow, llngCol + 13)), mfgAuxDados.get_TextMatrix(llngRow, llngCol + 13), 0))
                            ldblVlr(10, 2) = ldblVlr(10, 1)
                        Else
                            ldblVlr(6, 1) = 0
                            ldblVlr(6, 2) = 0
                            ldblVlr(7, 1) = 0
                            ldblVlr(7, 2) = 0
                            'ldblVlr(8, 1) = 0
                            'ldblVlr(8, 2) = 0
                            'ldblVlr(9, 1) = 0
                            'ldblVlr(9, 2) = 0
                        End If
                        If CShort(Len(lstrCod(llngCol - 1, 1))) > 0 Then
                            If Microsoft.VisualBasic.Left(lstrPre, 4) = "01B3" Then
                                If Microsoft.VisualBasic.Left(lstrPre, 5) <> "01B39" Then
                                    For lintAux = 1 To 5
                                        lstrCod(llngCol - 1, 1) = Replace(lstrCod(llngCol - 1, 1), "01B3" & IIf(lintAux > 1, lintAux - 1, 1), "01B3" & lintAux)
                                        If VerificaSeExiste(lstrCod(llngCol - 1, 1)) Then
                                            Call GravaTarifa(1, lstrCod(llngCol - 1, 1), TarifaToDescri(lstrCod(llngCol - 1, 1)), TarifaToLimitkWh(lstrCod(llngCol - 1, 1)), ldblVlr(llngCol - 1, 1), CDate(Me.cmbData.Text), ldblVlr(6, 1), ldblVlr(7, 1), ldblVlr(8, 1), ldblVlr(9, 1), ldblVlr(10, 1))
                                        Else
                                            Call GravaTarifa(2, lstrCod(llngCol - 1, 1), TarifaToDescri(lstrCod(llngCol - 1, 1)), TarifaToLimitkWh(lstrCod(llngCol - 1, 1)), ldblVlr(llngCol - 1, 1), CDate(Me.cmbData.Text), ldblVlr(6, 1), ldblVlr(7, 1), ldblVlr(8, 1), ldblVlr(9, 1), ldblVlr(10, 1))
                                        End If
                                    Next lintAux
                                Else
                                    For lintAux = 1 To 5
                                        lstrCod(llngCol - 1, 1) = Replace(lstrCod(llngCol - 1, 1), "01B39" & IIf(lintAux > 1, lintAux - 1, 1), "01B39" & lintAux)
                                        If VerificaSeExiste(lstrCod(llngCol - 1, 1)) Then
                                            Call GravaTarifa(1, lstrCod(llngCol - 1, 1), TarifaToDescri(lstrCod(llngCol - 1, 1)), TarifaToLimitkWh(lstrCod(llngCol - 1, 1)), ldblVlr(llngCol - 1, 1), CDate(Me.cmbData.Text), ldblVlr(6, 1), ldblVlr(7, 1), ldblVlr(8, 1), ldblVlr(9, 1), ldblVlr(10, 1))
                                        Else
                                            Call GravaTarifa(2, lstrCod(llngCol - 1, 1), TarifaToDescri(lstrCod(llngCol - 1, 1)), TarifaToLimitkWh(lstrCod(llngCol - 1, 1)), ldblVlr(llngCol - 1, 1), CDate(Me.cmbData.Text), ldblVlr(6, 1), ldblVlr(7, 1), ldblVlr(8, 1), ldblVlr(9, 1), ldblVlr(10, 1))
                                        End If
                                    Next lintAux
                                End If
                            Else
                                If VerificaSeExiste(lstrCod(llngCol - 1, 1)) Then
                                    Call GravaTarifa(1, lstrCod(llngCol - 1, 1), TarifaToDescri(lstrCod(llngCol - 1, 1)), TarifaToLimitkWh(lstrCod(llngCol - 1, 1)), ldblVlr(llngCol - 1, 1), CDate(Me.cmbData.Text), ldblVlr(6, 1), ldblVlr(7, 1), ldblVlr(8, 1), ldblVlr(9, 1), ldblVlr(10, 1))
                                Else
                                    Call GravaTarifa(2, lstrCod(llngCol - 1, 1), TarifaToDescri(lstrCod(llngCol - 1, 1)), TarifaToLimitkWh(lstrCod(llngCol - 1, 1)), ldblVlr(llngCol - 1, 1), CDate(Me.cmbData.Text), ldblVlr(6, 1), ldblVlr(7, 1), ldblVlr(8, 1), ldblVlr(9, 1), ldblVlr(10, 1))
                                End If
                            End If
                        End If
                        If CShort(Len(lstrCod(llngCol - 1, 2))) > 0 Then
                            If Microsoft.VisualBasic.Left(lstrPre, 4) = "01B3" Then
                                If Microsoft.VisualBasic.Left(lstrPre, 5) <> "01B39" Then
                                    For lintAux = 1 To 5
                                        lstrCod(llngCol - 1, 2) = Replace(lstrCod(llngCol - 1, 2), "01B3" & IIf(lintAux > 1, lintAux - 1, 1), "01B3" & lintAux)
                                        If VerificaSeExiste(lstrCod(llngCol - 1, 2)) Then
                                            Call GravaTarifa(1, lstrCod(llngCol - 1, 2), TarifaToDescri(lstrCod(llngCol - 1, 2)), TarifaToLimitkWh(lstrCod(llngCol - 1, 2)), ldblVlr(llngCol - 1, 2), CDate(Me.cmbData.Text), ldblVlr(6, 2), ldblVlr(7, 2), ldblVlr(8, 2), ldblVlr(9, 2), ldblVlr(10, 2))
                                        Else
                                            Call GravaTarifa(2, lstrCod(llngCol - 1, 2), TarifaToDescri(lstrCod(llngCol - 1, 2)), TarifaToLimitkWh(lstrCod(llngCol - 1, 2)), ldblVlr(llngCol - 1, 2), CDate(Me.cmbData.Text), ldblVlr(6, 2), ldblVlr(7, 2), ldblVlr(8, 2), ldblVlr(9, 2), ldblVlr(10, 2))
                                        End If
                                    Next lintAux
                                Else
                                    For lintAux = 1 To 5
                                        lstrCod(llngCol - 1, 2) = Replace(lstrCod(llngCol - 1, 2), "01B39" & IIf(lintAux > 1, lintAux - 1, 1), "01B39" & lintAux)
                                        If VerificaSeExiste(lstrCod(llngCol - 1, 2)) Then
                                            Call GravaTarifa(1, lstrCod(llngCol - 1, 2), TarifaToDescri(lstrCod(llngCol - 1, 2)), TarifaToLimitkWh(lstrCod(llngCol - 1, 2)), ldblVlr(llngCol - 1, 2), CDate(Me.cmbData.Text), ldblVlr(6, 2), ldblVlr(7, 2), ldblVlr(8, 2), ldblVlr(9, 2), ldblVlr(10, 2))
                                        Else
                                            Call GravaTarifa(2, lstrCod(llngCol - 1, 2), TarifaToDescri(lstrCod(llngCol - 1, 2)), TarifaToLimitkWh(lstrCod(llngCol - 1, 2)), ldblVlr(llngCol - 1, 2), CDate(Me.cmbData.Text), ldblVlr(6, 2), ldblVlr(7, 2), ldblVlr(8, 2), ldblVlr(9, 2), ldblVlr(10, 2))
                                        End If
                                    Next lintAux
                                End If
                            Else
                                If VerificaSeExiste(lstrCod(llngCol - 1, 2)) Then
                                    Call GravaTarifa(1, lstrCod(llngCol - 1, 2), TarifaToDescri(lstrCod(llngCol - 1, 2)), TarifaToLimitkWh(lstrCod(llngCol - 1, 2)), ldblVlr(llngCol - 1, 2), CDate(Me.cmbData.Text), ldblVlr(6, 2), ldblVlr(7, 2), ldblVlr(8, 2), ldblVlr(9, 2), ldblVlr(10, 2))
                                Else
                                    Call GravaTarifa(2, lstrCod(llngCol - 1, 2), TarifaToDescri(lstrCod(llngCol - 1, 2)), TarifaToLimitkWh(lstrCod(llngCol - 1, 2)), ldblVlr(llngCol - 1, 2), CDate(Me.cmbData.Text), ldblVlr(6, 2), ldblVlr(7, 2), ldblVlr(8, 2), ldblVlr(9, 2), ldblVlr(10, 2))
                                End If
                            End If
                        End If
                    Next llngCol
                Next llngRow
            End With

        Catch ex As Exception
            MsgBox(Err.Description)
            Exit Sub
        End Try

    End Sub

    Private Sub GravaTarifa(ByVal aintTipo As Short, ByVal astrCodigo As String, ByVal astrDescricao As String, ByVal astrLimitkWh As String, ByVal adblValor As Double, ByVal astrData As Date, ByVal adblVlrAM As Double, ByVal adblVlrVM As Double, ByVal adblVlrVBe As Double, ByVal adblVlrVBs As Double, ByVal adblVlrFioB As Double) '

        Dim objTarifa As New entidadeTarifa

        Try

            If (adblValor + adblVlrAM + adblVlrVM + adblVlrVBe + adblVlrVBs) > 0 Then
                'vQtdErros = vQtdErros + 1
                'Else

                objTarifa.NUM = astrCodigo
                objTarifa.DESCRI = astrDescricao
                objTarifa.UN = "NULL"
                objTarifa.LIMITKWH = astrLimitkWh
                objTarifa.VALOR = adblValor 'Replace(Convert.ToString(adblValor), ",", ".")
                objTarifa.DATANOVA = cmbData.Text
                objTarifa.REFERENCIA = Convert.ToDateTime(cmbData.Text)
                objTarifa.FLGCASC = "NULL"
                objTarifa.VALOR_AMARELA = adblVlrAM 'Convert.ToDouble(Replace(Convert.ToString(adblVlrAM), ",", "."))
                objTarifa.VALOR_VERMELHA = adblVlrVM 'Convert.ToDouble(Replace(Convert.ToString(adblVlrVM), ",", "."))
                objTarifa.VALOR_BASE_ECONOMICA = adblVlrVBe 'Convert.ToDouble(Replace(Convert.ToString(adblVlrVBe), ",", "."))
                objTarifa.VALOR_SEM_SUBSIDIO = adblVlrVBs 'Convert.ToDouble(Replace(Convert.ToString(adblVlrVBs), ",", "."))
                objTarifa.VALOR_TUSD_FIO_B = adblVlrFioB 'Convert.ToDouble(Replace(Convert.ToString(adblVlrFioB), ",", "."))

                If aintTipo = 1 Then

                    If Not lUpdate(objTarifa) Then
                        vQtdErros = vQtdErros + 1
                        'MessageBox.Show("Tarifa não pode ser alterada!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    Else
                        'MessageBox.Show("Tarifa alterada com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If

                Else

                    If Not lInsert(objTarifa) Then
                        vQtdErros = vQtdErros + 1
                        'MessageBox.Show("Tarifa não pode ser incluída!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    Else
                        ' MessageBox.Show("Tarifa incluída com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If

                End If

            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lClose()

        Try

            Close()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Function lInsert(ByRef objTarifa As entidadeTarifa) As Boolean
        Dim repositorioTarifa As New repositorioTarifa
        Try

            repositorioTarifa.Insert_1(objTarifa)

            Return True
        Catch ex As Exception
            MsgBox(Err.Description)
            Return False
        End Try
    End Function

    Private Function lUpdate(ByRef objTarifa As entidadeTarifa) As Boolean
        Dim repositorioTarifa As New repositorioTarifa
        Try

            repositorioTarifa.Update_1(objTarifa)

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub sstTarifas_Click(sender As Object, e As EventArgs) Handles sstTarifas.Click

        If sstTarifas.SelectedIndex() = 0 Then
            Me.txtValorGrupoA.Visible = True
            Me.txtValorGrupoBBr.Visible = False
            Me.txtValorGrupoBCn.Visible = False
            Me.txtValorGrupoBBx.Visible = False
        ElseIf sstTarifas.SelectedIndex() = 1 Then
            Me.txtValorGrupoBCn.Visible = True
            Me.txtValorGrupoA.Visible = False
            Me.txtValorGrupoBBr.Visible = False
            Me.txtValorGrupoBBx.Visible = False
        End If

    End Sub

    Private Sub sstSubTarifas_Click(sender As Object, e As EventArgs) Handles sstSubTarifas.Click
        If sstSubTarifas.SelectedIndex() = 0 Then
            Me.txtValorGrupoA.Visible = False
            Me.txtValorGrupoBBr.Visible = False
            Me.txtValorGrupoBCn.Visible = True
            Me.txtValorGrupoBBx.Visible = False
        ElseIf sstSubTarifas.SelectedIndex() = 1 Then
            Me.txtValorGrupoA.Visible = False
            Me.txtValorGrupoBBr.Visible = False
            Me.txtValorGrupoBCn.Visible = False
            Me.txtValorGrupoBBx.Visible = True
        ElseIf sstSubTarifas.SelectedIndex() = 2 Then
            Me.txtValorGrupoA.Visible = False
            Me.txtValorGrupoBBr.Visible = True
            Me.txtValorGrupoBCn.Visible = False
            Me.txtValorGrupoBBx.Visible = False
        End If
    End Sub



    'Private Sub cmdExcluir_Click(sender As Object, e As EventArgs) Handles cmdExcluir.Click
    '    Try

    '        Dim lstrsql As String

    '        If MsgBox("Deseja excluir as Tarifas com Refência de: " & VB6.Format(cmbData.Text, "dd/mm/yyyy"), MessageBoxButtons.YesNo, "Aviso") = vbYes Then

    '            lstrsql = ""
    '            lstrsql = "DELETE FROM tarifa WHERE REFERENCIA = '" & VB6.Format(cmbData.Text, "yyyy-mm-dd") & "'"
    '            objExecAtbd.QUERY_NAME = lstrsql
    '            repExecAtbd.Execute(objExecAtbd)

    '        End If

    '    Catch ex As Exception
    '        MsgBox(Err.Description)
    '        Exit Sub
    '    End Try


    'End Sub


#End Region


End Class