Imports System
Imports System.IO
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.XtraGrid.Views.Grid

Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.Xpo
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.Data
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Data.Filtering


Public Class InsertGridToRTF_VC
    Inherits DevExpress.ExpressApp.ViewController
    Dim _RichEditControl As RichEditControl
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "CRMEstate_Leads_DetailView" Then
            '_RichEditControl = TryCast(View, DetailView).GetItems(Of RichEditPropertyEditor)(0).RichEditUserControl.RichEditControl
        End If
    End Sub
    Private Sub aInsertGridToRTF_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aInsertGridToRTF.Execute
        Dim _DetailView As DetailView = CType(View, DetailView)
        Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(e.CurrentObject, CRMEstate_Leads)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _CRMEstate_Leads.ImmovablesPriceListHeader.ImmovablesGroups))

        If _CRMEstate_Leads.DocumentBody Is Nothing Then
            _CRMEstate_Leads.DocumentBody = _ImmovablesGroupsParameters.DocumentBody
            If _CRMEstate_Leads.DocumentBody Is Nothing Then
                _CRMEstate_Leads.DocumentBody = "{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}" & _
                        "{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;}{\*\listoverridetable}{\stylesheet {\ql\cf0 Normal;}" & _
                        "{\*\cs1\cf0 Default Paragraph Font;}{\*\cs2\sbasedon1\cf0 Line Number;}{\*\cs3\ul\cf1 Hyperlink;}}\sectd\pard\plain" & _
                        "\ql\f1\par\pard\plain\ql\f1\par\pard\plain\ql\f1\par\pard\plain\ql{\f2\fs18\cf0 imapartmentsim}\f2\fs18\cf2\par" & _
                        "\pard\plain\ql\f2\fs18\cf0\par\pard\plain\ql{\f2\fs18\cf0 imgaragesim}\f2\fs18\cf0\par\pard\plain\ql\f2\fs18\cf0\par" & _
                        "\pard\plain\ql{\f2\fs18\cf0 imofficesim}\f2\fs18\cf0\par\pard\plain\ql\f2\fs18\cf0\par\pard\plain\ql{\f2\fs18\cf0 imcontainerim}" & _
                        "\f2\fs18\cf0\par\pard\plain\ql\f1\par}"
            End If
        End If

        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("{\cf0 im}{\cf0 apartments}{\cf0 im}", "{\cf0 imapartmentsim}")
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("{\cf0 im}{\cf0 garages}{\cf0 im}", "{\cf0 imgaragesim}")
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("{\cf0 im}{\cf0 offices}{\cf0 im}", "{\cf0 imofficesim}")
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("{\cf0 im}{\cf0 container}{\cf0 im}", "{\cf0 imcontainerim}")

        '=========================== ColorTable =======================================
        Dim _actColorNum As Int32 = 0
        Dim _OrigColorTable As String
        Dim _ColorTable As String = (_CRMEstate_Leads.DocumentBody.Remove(0, (InStr(_CRMEstate_Leads.DocumentBody, "{\colortbl")) + "{\colortbl".Length - 1))
        _ColorTable = _ColorTable.Remove(InStr(_ColorTable, "}") - 1) '--Act Color table 
        Dim _ColorArray As String() = _ColorTable.Split(New [Char]() {";"c})
        Dim _CTable As New StringBuilder()
        _CTable.Append("{\colortbl")
        For _ik As Int32 = 0 To _ColorArray.Length - 2
            _CTable.Append(_ColorArray(_ik) & ";")
        Next
        _CTable.Append("\red240\green240\blue240 ;}")
        _OrigColorTable = "{\colortbl" & _ColorTable & "}"
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace(_OrigColorTable, _CTable.ToString())
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("improjectim", _CRMEstate_Leads.ImmovablesPriceListHeader.ImmovablesGroups.ShortName)
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("imlastnameim", _CRMEstate_Leads.LastName)
        _CRMEstate_Leads.DocumentBody = _CRMEstate_Leads.DocumentBody.Replace("imfirstnameim", _CRMEstate_Leads.FirstName)
        '=================================================================================

        '=========================== Apartments =======================================
        Dim _CRMEstate_Leads_Immovables_Apartments As ListPropertyEditor
        _CRMEstate_Leads_Immovables_Apartments = TryCast(View, DetailView).FindItem("Selected_Apartments")

        If _CRMEstate_Leads_Immovables_Apartments.ListView IsNot Nothing Then
            Dim _ListView_Apartments As ListView = CType(_CRMEstate_Leads_Immovables_Apartments.ListView, ListView)
            Dim _GridListEditor_Apartments As GridListEditor = TryCast(_ListView_Apartments.Editor, GridListEditor)
            Dim _Grid_Apartments As GridView = TryCast(_GridListEditor_Apartments.GridView, GridView)

            If _Grid_Apartments IsNot Nothing Then
                _CRMEstate_Leads.DocumentBody = RTFTableCreator(_Grid_Apartments, "imapartmentsim", _CRMEstate_Leads.DocumentBody)
            End If
        End If

        '=================================================================================

        '=========================== Offices ==========================================
        Dim _CRMEstate_Leads_Immovables_Offices As ListPropertyEditor
        _CRMEstate_Leads_Immovables_Offices = TryCast(View, DetailView).FindItem("Selected_Offices")

        If _CRMEstate_Leads_Immovables_Offices.ListView IsNot Nothing Then
            Dim _ListView_Offices As ListView = CType(_CRMEstate_Leads_Immovables_Offices.ListView, ListView)
            Dim _GridListEditor_Offices As GridListEditor = TryCast(_ListView_Offices.Editor, GridListEditor)
            Dim _Grid_Offices As GridView = TryCast(_GridListEditor_Offices.GridView, GridView)

            If _Grid_Offices IsNot Nothing Then
                _CRMEstate_Leads.DocumentBody = RTFTableCreator(_Grid_Offices, "imofficesim", _CRMEstate_Leads.DocumentBody)
            End If
        End If
        '=================================================================================

        '=========================== Garages ==========================================
        Dim _CRMEstate_Leads_Immovables_Garages As ListPropertyEditor
        _CRMEstate_Leads_Immovables_Garages = TryCast(View, DetailView).FindItem("Selected_Garages")
        If _CRMEstate_Leads_Immovables_Garages.ListView IsNot Nothing Then
            Dim _ListView_Garages As ListView = CType(_CRMEstate_Leads_Immovables_Garages.ListView, ListView)
            Dim _GridListEditor_Garages As GridListEditor = TryCast(_ListView_Garages.Editor, GridListEditor)
            Dim _Grid_Garages As GridView = TryCast(_GridListEditor_Garages.GridView, GridView)

            If _Grid_Garages IsNot Nothing Then
                _CRMEstate_Leads.DocumentBody = RTFTableCreator(_Grid_Garages, "imgaragesim", _CRMEstate_Leads.DocumentBody)
            End If
        End If
        '=================================================================================

        '=========================== Container ==========================================
        Dim _CRMEstate_Leads_Immovables_Container As ListPropertyEditor
        _CRMEstate_Leads_Immovables_Container = TryCast(View, DetailView).FindItem("Selected_Container")
        If _CRMEstate_Leads_Immovables_Container.ListView IsNot Nothing Then
            Dim _ListView_Container As ListView = CType(_CRMEstate_Leads_Immovables_Container.ListView, ListView)
            Dim _GridListEditor_Container As GridListEditor = TryCast(_ListView_Container.Editor, GridListEditor)
            Dim _Grid_Container As GridView = TryCast(_GridListEditor_Container.GridView, GridView)

            If _Grid_Container IsNot Nothing Then
                _CRMEstate_Leads.DocumentBody = RTFTableCreator(_Grid_Container, "imcontainerim", _CRMEstate_Leads.DocumentBody)
            End If
        End If
        '=================================================================================
    End Sub
    Private Sub aInsertGridToRTFCurrentPos_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aInsertGridToRTFCurrentPos.Execute
        'Dim _DetailView As DetailView = CType(View, DetailView)
        Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(e.CurrentObject, CRMEstate_Leads)

        Dim _CRMEstate_Leads_Immovables As ListPropertyEditor
        _CRMEstate_Leads_Immovables = TryCast(View, DetailView).FindItem("CRMEstate_Leads_Immovables")

        Dim _ListView As ListView = CType(_CRMEstate_Leads_Immovables.ListView, ListView)
        Dim _GridListEditor As GridListEditor = TryCast(_ListView.Editor, GridListEditor)
        Dim _Grid As GridView = TryCast(_GridListEditor.GridView, GridView)


        _RichEditControl.Document.InsertText(_RichEditControl.Document.CaretPosition, "LilaLiba")


        Dim _RTFTable As String = RTFTableCreatorCurrentPos(_Grid)

        Dim _DocumentBody_NEW As String = _CRMEstate_Leads.DocumentBody
        _DocumentBody_NEW = _CRMEstate_Leads.DocumentBody.Replace("LilaLiba", _RTFTable)
        _CRMEstate_Leads.DocumentBody = ""
        _CRMEstate_Leads.DocumentBody = _DocumentBody_NEW

        ' _RichEditControl.Document.InsertRtfText(_DocumentPosition, RTFTableCreatorCurrentPos(_Grid))
        '_RichEditControl.Document.CaretPosition = _RichEditControl.Document.Range.End


    End Sub
    Private Function RTFTableCreatorCurrentPos(_Grid As GridView) ', _DocumentBody As String) As String
        Dim _RTFtext As New StringBuilder
        Dim _RowNum As Integer = _Grid.RowCount
        Dim _ColumnsNum As Integer = _Grid.Columns.Count
        Dim irowIndex As Integer = 0
        '--Fejléc kiírása RTFként--

        _RTFtext.Append("\par\pard\plain\ql\par\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                        "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
        For _ik As Integer = 0 To _ColumnsNum - 1
            If _Grid.Columns(_ik).Visible = True Then
                _RTFtext.Append("\clvertalt\clcbpat2\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
            End If
        Next
        For _ik As Integer = 0 To _ColumnsNum - 1
            If _Grid.Columns(_ik).Visible = True Then
                _RTFtext.Append("\pard\plain\ql\intbl{\b\cf0" & _Grid.Columns(_ik).Caption & "}\b\cell")
            End If
        Next

        _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                        "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
        For _ik As Integer = 0 To _ColumnsNum - 1
            If _Grid.Columns(_ik).Visible = True Then
                _RTFtext.Append("\clvertalt\clcbpat2\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
            End If
        Next
        irowIndex += 1

        '' --Adatsorok kiírása RTFként--
        For _ik As Integer = 0 To _RowNum - 2

            _RTFtext.Append("\row")
            For _jk As Integer = 0 To _ColumnsNum - 2
                If _Grid.Columns(_jk).Visible = True Then
                    _RTFtext.Append("\pard\plain\ql\intbl{\cf0" & (_Grid.GetRowCellValue(_ik, _Grid.Columns(_jk)).ToString()) & "}\cell")
                End If
            Next

            _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                            "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

            For _jk As Integer = 0 To _ColumnsNum - 1
                If _Grid.Columns(_jk).Visible = True Then
                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                    "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                End If
            Next
            irowIndex += 1
        Next

        ''--Utolsó sor --
        _RTFtext.Append("\row")
        For _jk As Integer = 0 To _ColumnsNum - 2
            If _Grid.Columns(_jk).Visible = True Then
                _RTFtext.Append("\pard\plain\ql\intbl{\cf0" & (_Grid.GetRowCellValue(_RowNum - 1, _Grid.Columns(_jk)).ToString()) & "}\cell")
            End If
        Next

        _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\lastrow\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                           "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

        For _jk As Integer = 0 To _ColumnsNum - 1
            If _Grid.Columns(_jk).Visible = True Then
                _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
            End If
        Next
        _RTFtext.Append("\row\pard\plain\ql\par")

        '_DocumentBody_NEW = _DocumentBody.Replace(_Tag, _RTFtext.ToString())
        '_DocumentBody_NEW = _DocumentBody_NEW.Replace("{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;}", "{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;\red192\green192\blue192 ;}")

        Return _RTFtext.ToString()
    End Function
    Private Sub aRestoreDocumentTemplate_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRestoreDocumentTemplate.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(View.SelectedObjects(0), CRMEstate_Leads)
            If _CRMEstate_Leads Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _ocur.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups=?", _
                                                                                _CRMEstate_Leads.ImmovablesPriceListHeader.ImmovablesGroups))

            If _ImmovablesGroupsParameters Is Nothing Then Throw New Exception("*Nincs beállítva ingatlancsoport paraméter!")

            _CRMEstate_Leads.DocumentBody = _ImmovablesGroupsParameters.DocumentBody
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try

    End Sub

    '-------------------Functions----------------------
    Function RTFTableCreator(_Grid As GridView, _Tag As String, _DocumentBody As String, Optional _FontSize As String = "8", _
                                Optional _FontType As String = "Tahoma", Optional _RGB As String = "240,240,240") As String


        Dim _DocumentBody_NEW As String
        Dim _HeaderFillColor As Int32 ' -- Current table header fill color index in colot table

        If _RGB <> "240,240,240" Then
            Dim _Parameters As String() = ColorTablereplacement(_DocumentBody, _RGB)
            _HeaderFillColor = Convert.ToInt32(_Parameters(0))
            _DocumentBody = _Parameters(1)
        Else
            'Dim _ColorTable As String = (_DocumentBody.Remove(0, (InStr(_DocumentBody, "{\colortbl")) + "{\colortbl".Length - 1))
            '_ColorTable = _ColorTable.Remove(InStr(_ColorTable, "}") - 1) '--Act Color table 
            'Dim _ColorArray As String() = _ColorTable.Split(New [Char]() {";"c})
            'For _ik = 0 To _ColorArray.Length - 1
            '    If _ColorArray(_ik) = "\red240\green240\blue240 " Then
            '        _HeaderFillColor = _ik
            '        Exit For
            '    End If
            'Next
            _HeaderFillColor = 2
        End If

        If _Grid.DataRowCount > 0 Then
            Dim _FontSizeNUM As Int32 = (Convert.ToInt32(_FontSize) * 2) ' -- Font size in half-points (the default is 16). => Size 8!!!
            Dim _RTFtext As New StringBuilder
            Dim _RowNumFull As Integer = _Grid.RowCount
            Dim _RowNum As Integer = _Grid.DataRowCount
            Dim _ColumnsNum As Integer = _Grid.Columns.Count
            Dim irowIndex As Int32 = 0
            Dim _LastCellIndex As Int32 = 0

            '--Header to RTF format--
            '=================================================================================
            _RTFtext.Append("\pard\plain\ql\par\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                            "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\clvertalt\clcbpat2\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                    "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
                If _LastCellIndex < _ik Then
                    _LastCellIndex = _ik
                End If
            Next
            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\pard\plain\ql\intbl{\b\fs" & _FontSizeNUM & "\cf0 " & _Grid.Columns.View.VisibleColumns(_ik).Caption & "}\b\cell")
            Next

            _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                            "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\clvertalt\clcbpat" & _HeaderFillColor & "\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                    "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
            Next
            irowIndex += 1
            '' --Data to RTF format--
            '=================================================================================
            For _ik As Int16 = 0 To _RowNumFull - 2
                If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_ik)) = True Then
                    _RTFtext.Append("\row")

                    _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetGroupRowDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_ik)).ToString) & "}\cell")


                    _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                    "\trleft-108\trftsWidth1\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108\tblindtype3\tblind0")

                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth9619" & _
                                             "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _LastCellIndex)

                    irowIndex += 1
                Else
                    _RTFtext.Append("\row")

                    For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                        If _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Double" And _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Boolean" Then

                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetRowCellDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk))) & "}\cell")
                        ElseIf _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName = "System.Boolean" Then
                            If _Grid.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk)) = True Then
                                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Van" & "}\cell")
                            Else
                                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Nincs" & "}\cell")
                            End If
                        Else
                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0\qr " & LTrim(RTrim(Format(_Grid.Columns.View.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk)), "#,##0.00"))) & "}\qr\cell")

                        End If
                    Next

                    _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                    "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

                    For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1

                        _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                        "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                    Next
                    irowIndex += 1
                End If
            Next
            '--Last row --
            '=================================================================================
            If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)) = True Then
                _RTFtext.Append("\row")
                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetGroupRowDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)).ToString) & "}\cell")

                _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                "\trleft-108\trftsWidth1\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108\tblindtype3\tblind0")

                _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth9619" & _
                                         "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _LastCellIndex)
            Else
                _RTFtext.Append("\row")
                For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                    If _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Double" And _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Boolean" Then

                        _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.Columns.View.GetRowCellDisplayText(_RowNum - 1, _Grid.Columns.View.VisibleColumns(_jk))) & "}\cell")
                    ElseIf _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName = "System.Boolean" Then
                        If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)) = True Then

                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Van" & "}\cell")
                        Else
                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Nincs" & "}\cell")
                        End If
                    Else
                        _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0\qr " & LTrim(RTrim(Format(_Grid.Columns.View.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1), _Grid.Columns.View.VisibleColumns(_jk)), "#,##0.00"))) & "}\qr\cell")
                    End If
                Next

                _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\lastrow\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                   "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
                ''---
                For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1

                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                    "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                Next
            End If

            _RTFtext.Append("\row\pard\plain\ql")
            _DocumentBody_NEW = _DocumentBody.Replace(_Tag, _RTFtext.ToString())
            If _DocumentBody_NEW.Contains("\fonttbl{\f0 Times New Roman;") Then
                _DocumentBody_NEW = _DocumentBody_NEW.Replace("\fonttbl{\f0 Times New Roman;", "\fonttbl{\f0 " & _FontType & ";") 'Font table remake
            End If
        Else
            _DocumentBody_NEW = _DocumentBody.Replace(_Tag, " ")
        End If

        Return _DocumentBody_NEW
    End Function
    Function ColorTablereplacement(_DocumentBody As String, _RGB As String) As String()
        Dim _Parameters(1) As String
        Dim _RGBArray As String() = _RGB.Split(New [Char]() {","c})

        Dim _actColorNum As Int32 = -1
        Dim _ColorTable As String = (_DocumentBody.Remove(0, (InStr(_DocumentBody, "{\colortbl")) + "{\colortbl".Length - 1))
        _ColorTable = _ColorTable.Remove(InStr(_ColorTable, "}") - 1) '--Act Color table 
        Dim _ColorArray As String() = _ColorTable.Split(New [Char]() {";"c})

        For _ik As Int32 = 0 To _ColorArray.Length - 1
            If _ColorArray(_ik) = "\red" & _RGBArray(0) & "\green" & _RGBArray(1) & "\blue" & _RGBArray(2) & " " Then
                _actColorNum = _ik
            End If
        Next
        If _actColorNum = -1 Then
            Dim _CTable As New StringBuilder()
            _CTable.Append("{\colortbl")
            For _ik As Int32 = 0 To _ColorArray.Length - 2
                _CTable.Append(_ColorArray(_ik) & ";")
            Next
            _CTable.Append("\red" & _RGBArray(0) & "\green" & _RGBArray(1) & "\blue" & _RGBArray(2) & " ;}")
            Dim _OrigColorTable As String = "{\colortbl" & _ColorTable & "}"
            _DocumentBody = _DocumentBody.Replace(_OrigColorTable, _CTable.ToString())
            _Parameters(0) = _ColorArray.Length - 1
            _Parameters(1) = _DocumentBody
        Else
            _Parameters(0) = _actColorNum
            _Parameters(1) = _DocumentBody
        End If
        Return _Parameters
    End Function
End Class
