Imports System.Text
Imports System.IO
Imports DD_LIB_Standards
Public Class frmDocLink_to_Record
    Dim DT_TREEVIEW_PER_CONTROLS As DataTable
    Dim TreeView_perControl As Boolean = False
    Dim DT_TREEVIEW_CONFIGURATION As DataTable
    Private DT_VWPMO_CONSTRUCTOR_FORMS As DataTable

    Private TREEVIEW_IMAGELIST As ImageList
    Private NODE_NAVIGATION As Boolean = False
    Private NODE_NAVIGATION_LOADED As Boolean = False
    Private ENTITY_LOADED As Boolean = False
    Private ENTITY_ID As Integer
    Private RECORD_ID As Integer
    ' Private _objecttype As String
    Private Class ClassConstructor
        Public title As String
        Public id As Integer

        Public Overrides Function ToString() As String
            Return Me.title
        End Function
    End Class

    Private CONSTRUCTOR_DETAIL_ID, CONSTRUCTOR_ID, FORM_ID As Integer

    Public Sub New() 'fileName As String, Objecttype As String)
        InitializeComponent()
        If ClassWindreamDocGrid.DT_RESULTFILES.Rows.Count = 1 Then
            txtFileInfo.Text = ClassWindreamDocGrid.DT_RESULTFILES.Rows(0).Item("DOC_PATH")
        Else
            txtFileInfo.Text = String.Format("{0} files selected for Record2Link", ClassWindreamDocGrid.DT_RESULTFILES.Rows.Count)
        End If
        'txtFilename.Text = fileName
        '_objecttype = Objecttype
    End Sub

    Private Function GetConstructorForms() As DataTable
        Try
            Dim SQL As String = String.Format("SELECT GUID, dbo.FNPMO_GETOBJECTCAPTION('{0}', 'CONSTR_MENUE' + CONVERT(VARCHAR(5), GUID), {1}) AS FORM_TITLE, SEQUENCE_MENU, ADDED_WHO, ADDED_WHEN, CHANGED_WHO," & _
           "CHANGED_WHEN, MENU_IMG, SHORT_TITLE, LANGUAGE, " & _
           "'CONSTR_MENUE' + CONVERT(VARCHAR(5), GUID) as OBECTNAME FROM TBPMO_FORM_CONSTRUCTOR where GUID in (SELECT CONSTRUCT_ID FROM TBPMO_CONSTRUCTOR_USER WHERE HOME_MENUE = 1 AND USER_ID = {2}) ORDER BY SEQUENCE_MENU", USER_LANGUAGE, CURRENT_SCREEN_ID, USER_GUID)
            Dim dt As DataTable = ClassDatabase.Return_Datatable(SQL)
            Return dt
        Catch ex As Exception
            MsgBox("Constructor Forms could not be loaded in LinkToRecord: " & vbNewLine & ex.Message)
            Return Nothing
        End Try
    End Function

    Private Sub frmWD_Link_to_Record_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ClassDOC_SEARCH.REFRESH_DOC_TABLE_RESULTS()
    End Sub

    Private Sub frmWD_Link_to_Record_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim constructorForms As DataTable = GetConstructorForms()
        Dim selected As Integer = 0

        If constructorForms.Rows.Count = 0 Then
            Exit Sub
        End If

        For Each row As DataRow In constructorForms.Rows
            Dim constructor As New ClassConstructor
            constructor.title = row.Item("FORM_TITLE")
            constructor.id = row.Item("GUID")
            cmbConstructorForms.Items.Add(constructor)

            ' Von der aktuell geöffneten ConstructorForm den Index in Combobox speichern
            If constructor.id = CURRENT_CONSTRUCTOR_ID Then
                selected = cmbConstructorForms.Items.Count - 1
            End If
        Next
        If USER_LANGUAGE = "de-DE" Then
            Me.Text = CURRENT_DT_SELECTED_FILES.Rows.Count.ToString & " Datei(en) mit Datensatz verknüpfen:"
        Else
            Me.Text = "Link " & CURRENT_DT_SELECTED_FILES.Rows.Count.ToString & " files with record:"
        End If
        ' Den index der aktuell geöffneten ConstructorForm setzen
        cmbConstructorForms.SelectedIndex = selected
    End Sub

    Sub Load_TreeViewEbene(constructorId As Integer)
        Cursor = Cursors.WaitCursor
        trvwMain.Nodes.Clear()
        If NODE_NAVIGATION = True Then
            Dim newTreeview As TreeView
            newTreeview = ClassNodeNavigation.CreateTreeViewNodesNew1(DT_TREEVIEW_PER_CONTROLS, TREEVIEW_IMAGELIST)

            If Not IsNothing(newTreeview) Then
                For Each node As TreeNode In newTreeview.Nodes
                    trvwMain.Nodes.Add(node.Clone)
                Next
            End If
            
        Else
            Try
                Dim sel_sql = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                                "from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID IN (1,4) AND PARENT_ID = 0 AND CONSTRUCT_ID = {2} AND SCREEN_ID = {3} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, constructorId, CURRENT_SCREEN_ID)

                Dim DT As DataTable = ClassDatabase.Return_Datatable(sel_sql)
                If DT.Rows.Count >= 1 Then
                    trvwMain.Nodes.Clear()
                    For Each rowEbene0 As DataRow In DT.Rows
                        Dim Node As TreeNode
                        Node = trvwMain.Nodes.Add("E1: " & rowEbene0.Item("CAPTION").ToString)
                        Node.Tag = rowEbene0.Item("GUID")
                        'Jetzt die Ebene1Knoten einhängen
                        sel_sql = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                                "from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID in (2,5) AND PARENT_ID = {2} AND CONSTRUCT_ID = {3} AND SCREEN_ID = {4} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, rowEbene0.Item("FORM_ID"), constructorId, CURRENT_SCREEN_ID)

                        Dim dtEbene1 As DataTable = ClassDatabase.Return_Datatable(sel_sql)
                        If dtEbene1.Rows.Count > 0 Then
                            For Each rowEbene1 As DataRow In dtEbene1.Rows
                                Dim Node1 As TreeNode
                                Node1 = Node.Nodes.Add("E2: " & rowEbene1.Item("CAPTION").ToString)
                                Node1.Tag = rowEbene1.Item("GUID")
                                'Jetzt die Ebene2 Knoten einhängen
                                sel_sql = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                               "from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID in (3,5) AND PARENT_ID = {2} AND CONSTRUCT_ID = {3} AND SCREEN_ID = {4} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, rowEbene1.Item("FORM_ID"), constructorId, CURRENT_SCREEN_ID)

                                Dim dtEbene2 As DataTable = ClassDatabase.Return_Datatable(sel_sql)
                                If dtEbene2.Rows.Count > 0 Then
                                    For Each rowEbene2 As DataRow In dtEbene2.Rows
                                        Dim Node2 As TreeNode
                                        Node2 = Node1.Nodes.Add("E3: " & rowEbene2.Item("CAPTION").ToString)
                                        Node2.Tag = rowEbene2.Item("GUID")
                                    Next
                                End If

                            Next

                        End If
                    Next
                    trvwMain.ExpandAll()
                    trvwMain.Nodes(0).EnsureVisible()
                Else
                    MsgBox("Achtung Konfigurationsfehler: Mehrere oder keine Level 0 Entität gefunden!" & "Korrigieren Sie bitte Ihre Eingaben", MsgBoxStyle.Exclamation)
                End If
            Catch ex As Exception
                MsgBox("Error in Load_TreeViewEbene in LinkToRecord:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If

        Cursor = Cursors.Default
    End Sub

    Public Function Get_Grid_Sql(FormId As Integer, ConstructorDetailID As Integer) As String
        Try
            Dim ResultSQL, ResultViewName As String
            Dim RecordId As Integer

            If LogErrorsOnly = False Then ClassLogger.Add(" >> GridType = Grid", False)
            ResultViewName = "VWTEMP_PMO_FORM" & FormId.ToString
            ResultSQL = "SELECT T.* FROM " & ResultViewName & " T"

            Try
                Dim sql = String.Format("SELECT COUNT(*) FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE USER_ID = {0} AND CONSTR_DET_ID = {1} AND SQL_COMMAND IS NOT NULL AND SQL_COMMAND <> ''", USER_GUID, ConstructorDetailID)
                Dim exists = ClassDatabase.Execute_Scalar(sql)
                If exists = 1 Then
                    sql = String.Format("SELECT SQL_COMMAND FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE USER_ID = {0} AND CONSTR_DET_ID = {1}", USER_GUID, ConstructorDetailID)
                    Dim result = ClassDatabase.Execute_Scalar(sql)
                    If Not IsNothing(result) Then
                        ResultSQL = ResultSQL & " " & result.ToString
                        'ResultSQL = ResultSQL.ToUpper.Replace("@RECORDID", RECORD_ID)
                        ResultSQL = ResultSQL.ToUpper.Replace("@USER_ID", USER_GUID)
                    End If
                End If

                Return ResultSQL
            Catch ex As Exception
                ClassLogger.Add("Error in Get_Grid_Sql in LinkToRecord: " & vbNewLine & ex.Message, True)
                MsgBox("Error in Get_Grid_Sql in LinkToRecord: " & vbNewLine & ex.Message)
            End Try
        Catch ex As Exception
            ClassLogger.Add("Error in Get_Grid_Sql: " & vbNewLine & ex.Message, True)
            MsgBox("Error in Get_Grid_Sql: " & vbNewLine & ex.Message)
        End Try
    End Function

    Public foundNode As TreeNode = Nothing
    Private Sub cmbConstructorForms_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbConstructorForms.SelectedIndexChanged
        Dim constructor As ClassConstructor = DirectCast(cmbConstructorForms.SelectedItem, ClassConstructor)

        If cmbConstructorForms.SelectedIndex <> -1 Then
            CONSTRUCTOR_ID = constructor.id
            Dim SQL = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                       "FROM VWPMO_CONSTRUCTOR_FORMS WHERE SCREEN_ID = {1} AND  CONSTRUCT_ID = {2} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, CONSTRUCTOR_ID)

            DT_VWPMO_CONSTRUCTOR_FORMS = ClassDatabase.Return_Datatable(SQL, True)

            SQL = String.Format("select * from VWPMO_STRUCTURE_NODES where ENTITY_ID IN (SELECT FORM_ID FROM VWPMO_CONSTRUCTOR_FORMS WHERE CONSTRUCT_ID = {0})", CONSTRUCTOR_ID)
            Dim DT As DataTable = ClassDatabase.Return_Datatable(Sql)
            If DT.Rows.Count > 1 Then
                TreeView_perControl = True
                DT_TREEVIEW_PER_CONTROLS = DT
                SQL = String.Format("select * from TBPMO_STRUCTURE_NODES_CONFIGURATION where ENTITY_ID IN (SELECT FORM_ID FROM VWPMO_CONSTRUCTOR_FORMS WHERE CONSTRUCT_ID = {0})", CONSTRUCTOR_ID)
                DT_TREEVIEW_CONFIGURATION = ClassDatabase.Return_Datatable(Sql)

                Try
                    TREEVIEW_IMAGELIST = New ImageList
                    For Each row As DataRow In DT_TREEVIEW_CONFIGURATION.Rows
                        Try
                            Dim bimage = row.Item("NODE_IMAGE")
                            If Not IsDBNull(bimage) Then
                                Dim Node_image = ByteArrayToBitmap(bimage)
                                TREEVIEW_IMAGELIST.Images.Add(row.Item("GUID").ToString & "#" & row.Item("ENTITY_ID"), Node_image)
                            End If
                        Catch ex As Exception

                        End Try

                    Next
                    trvwMain.ImageList = TREEVIEW_IMAGELIST
                Catch ex As Exception
                    MsgBox("Error in Adding NodeConfigurations to TreeView: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                End Try


                NODE_NAVIGATION = True
                NODE_NAVIGATION_LOADED = False
                grvwGrid.Columns.Clear()
                dgEntityRecords.DataSource = Nothing
            Else
                TreeView_perControl = False
                NODE_NAVIGATION = False
            End If


            ' Entitäten laden
            Load_TreeViewEbene(constructor.id)

            foundNode = Nothing
            SelectNodeRecursive(trvwMain, trvwMain.Nodes, CURRENT_CONSTRUCTOR_DETAIL_ID)

            If IsNothing(foundNode) Then
                'Erste Entität selektieren
                trvwMain.SelectedNode = trvwMain.Nodes.Item(0)
            Else
                'Aktuelle Entität selektieren
                trvwMain.SelectedNode = foundNode
            End If
        End If
    End Sub

    Public Sub SelectNodeRecursive(treeview As TreeView, nodes As TreeNodeCollection, constructorDetailId As Integer)
        Try
            For Each node As TreeNode In nodes
                Dim id As Integer = node.Tag
                If id = constructorDetailId Then
                    foundNode = node
                    Exit Sub
                Else
                    SelectNodeRecursive(treeview, node.Nodes, constructorDetailId)
                End If
            Next
        Catch ex As Exception

        End Try

    End Sub

    Private Sub trvwMain_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles trvwMain.AfterSelect
        Try

            Dim dt As DataTable
            If NODE_NAVIGATION = False Then
                SetSelectedNodeStyle(trvwMain.Nodes)

                CONSTRUCTOR_DETAIL_ID = Integer.Parse(e.Node.Tag)
                Dim SQL As String = String.Format("SELECT FORM_ID FROM VWPMO_CONSTRUCTOR_FORMS WHERE GUID = {0} AND CONSTRUCT_ID = {1}", CONSTRUCTOR_DETAIL_ID, CONSTRUCTOR_ID)
                FORM_ID = ClassDatabase.Execute_Scalar(SQL)
                SQL = Get_Grid_Sql(FORM_ID, CONSTRUCTOR_DETAIL_ID)
                dt = ClassDatabase.Return_Datatable(SQL)
                dgEntityRecords.DataSource = dt
                grvwGrid.PopulateColumns()
                dgEntityRecords.RefreshDataSource()
            Else
                RECORD_ID = 0
                Dim SelectedNode As TreeNode = TryCast(trvwMain.SelectedNode, TreeNode)
                If SelectedNode IsNot Nothing Then
                    SelectedNode.NodeFont = New Font(trvwMain.Font.FontFamily, trvwMain.Font.Size, FontStyle.Bold)
                    SelectedNode.Text = SelectedNode.Text
                Else
                    Exit Sub
                End If


                If NODE_NAVIGATION_LOADED = False Then
                    ENTITY_ID = ClassNodeNavigation.Return_ENTITY_FOR_TAG(SelectedNode.Tag)
                    Dim SQL As String '= String.Format("SELECT FORM_ID FROM VWPMO_CONSTRUCTOR_FORMS WHERE CONSTRUCT_ID = {0} AND FORM_ID = {1}", CONSTRUCTOR_ID, ENTITY_ID)
                    SQL = Get_Grid_Sql(ENTITY_ID, CONSTRUCTOR_DETAIL_ID)
                    dt = ClassDatabase.Return_Datatable(SQL)
                    If IsNothing(dt) Then
                        Exit Sub
                    End If
                    dgEntityRecords.DataSource = dt
                    grvwGrid.PopulateColumns()
                    dgEntityRecords.RefreshDataSource()
                    NODE_NAVIGATION_LOADED = True
                End If

                If LogErrorsOnly = False Then ClassLogger.Add(" >> NodeTag: " & SelectedNode.Tag.ToString, False)
                If SelectedNode.Tag.ToString.Contains("RECORD-ID") Then
                    Dim Record = ClassNodeNavigation.Return_RECORD_forTag(SelectedNode.Tag)
                    If Record <> 0 Then
                        txtRecordId.Text = Record
                        RECORD_ID = Record
                    End If

                End If
                If Not IsNothing(dgEntityRecords.DataSource) Then
                    Dim RecordId As Integer = grvwGrid.GetFocusedRowCellValue(grvwGrid.Columns(0))
                    txtRecordId.Text = RecordId
                Else
                    txtRecordId.Text = ""
                    RECORD_ID = 0
                End If


            End If
            ' Datasource neu setzen und columns laden

            Dim constructor = DirectCast(cmbConstructorForms.Items.Item(cmbConstructorForms.SelectedIndex), ClassConstructor)
            Dim constructorName As String = constructor.title
            If Not IsNothing(dgEntityRecords.DataSource) Then
                Load_Grid_Layout(constructorName)
                grvwGrid.Columns.Item("Form-ID").Visible = False
                grvwGrid.Columns.Item("ROW_COLOR").Visible = False
            End If

            If RECORD_ID > 0 Then
                Dim rowHandle = grvwGrid.LocateByValue(0, grvwGrid.Columns("Record-ID"), RECORD_ID)
                grvwGrid.FocusedRowHandle = rowHandle
            End If

        Catch ex As Exception
            MsgBox("Entität " + CONSTRUCTOR_DETAIL_ID + " konnte nicht geladen werden: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Exit Sub
        End Try
    End Sub
    Function GetSafeFilename(filename As String)
        Return String.Join("_", filename.Split(Path.GetInvalidFileNameChars()))
    End Function

    Private Function Get_Grid_Layout_Filename(EntityName As String)
        Dim Filename As String = String.Format("{0}-{1}-{2}-UserLayout.xml", grvwGrid.Name, GetSafeFilename(EntityName), CONSTRUCTOR_ID)
        Return System.IO.Path.Combine(Application.UserAppDataPath(), Filename)
    End Function
    Public Sub SetSelectedNodeStyle(tncoll As TreeNodeCollection)
        For Each node As TreeNode In tncoll

            If node.IsSelected Then
                node.NodeFont = New Font(trvwMain.Font.FontFamily, trvwMain.Font.Size, FontStyle.Bold)
                node.Text = node.Text
            Else
                node.NodeFont = New Font(trvwMain.Font.FontFamily, trvwMain.Font.Size, FontStyle.Regular)
            End If

            If node.Nodes.Count > 0 Then
                SetSelectedNodeStyle(node.Nodes)
            End If

        Next
        trvwMain.Refresh()
    End Sub
    Sub Load_Grid_Layout(constructorName As String)
        Try
            Dim XMLPath = Get_Grid_Layout_Filename(constructorName)
            If File.Exists(XMLPath) Then
                grvwGrid.RestoreLayoutFromXml(XMLPath)
                grvwGrid.GuessAutoFilterRowValuesFromFilter()
            End If
        Catch ex As Exception
            MsgBox("Error in Load_Grid_Layout: " & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub GridView1_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles grvwGrid.FocusedRowChanged
        If Not IsNothing(dgEntityRecords.DataSource) Then
            Dim RecordId As Integer = grvwGrid.GetFocusedRowCellValue(grvwGrid.Columns(0))
            txtRecordId.Text = RecordId
        End If
    End Sub

    Private Sub btnLink_Click(sender As Object, e As EventArgs) Handles btnLink.Click
        If clsWindream.Create_Session = False Then
            MsgBox("No Seesion to windream created! Check Your connection!", MsgBoxStyle.Critical)
            Exit Sub
        End If
        Dim RecordId As Integer

        If txtRecordId.Text.Trim.Count > 0 Then
            RecordId = Integer.Parse(txtRecordId.Text)
            Try
                For Each row As DataRow In CURRENT_DT_SELECTED_FILES.Rows


                    CURRENT_DOC_ID = row.Item("DOC_ID")
                    Dim DOC_PATH = row.Item("FILEPATH")
                    Dim _objecttype = row.Item("OBJECTTYPE")
                    clsWindream.MY_WDOBJECTTYPE = _objecttype
                    Dim ins = String.Format("INSERT INTO TBPMO_DOC_RECORD_LINK (RECORD_ID,DOC_ID,COMMENT,ADDED_WHO) VALUES ({0},{1},'{2}','{3}')", RecordId, CURRENT_DOC_ID, "MANUAL RECORD-LINK", USER_USERNAME)
                    If ClassDatabase.Execute_non_Query(ins) = False Then
                        MsgBox("Unexpected Error in Inserting Record-Doc Link. Please check logfile!", MsgBoxStyle.Critical)
                    Else
                        If LICENSE_SITE_PROXY = True And DD_LIB_Standards.clsDatabase.DB_PROXY_INITIALIZED Then
                            Dim proc = String.Format("EXEC PRPROXY_DOC_CHECK_DOC_REC_LINK {0},{1}", CURRENT_DOC_ID, RecordId)
                            ClassDatabase.Execute_non_Query(proc, True)
                        End If
                    End If
                    Dim sql = String.Format("SELECT * FROM TBPMO_WD_OBJECTTYPE WHERE OBJECT_TYPE = '{0}'", _objecttype)
                    Dim DT_OBJTYPE As DataTable = ClassDatabase.Return_Datatable(sql, True)
                    If DT_OBJTYPE.Rows.Count = 1 Then
                        Dim Entity_Index = DT_OBJTYPE.Rows(0).Item("IDXNAME_ENTITYID")
                        Dim Record_Index = DT_OBJTYPE.Rows(0).Item("IDXNAME_RECORDID")
                        Dim ENT_ID = ClassDatabase.Execute_Scalar(String.Format("SELECT FORM_ID FROM TBPMO_RECORD WHERE GUID = {0}", RecordId))
                        If ENT_ID > 1 Then
                            If clsWD_SET.IndexFile(DOC_PATH, Entity_Index, ENT_ID, _objecttype) = False Then
                                Dim msg = "Die Entität-ID konnte nicht gesetzt werden! Bitte überprüfen Sie die Logfile"
                                If USER_LANGUAGE <> "de-DE" Then
                                    msg = "Could not index Entity-ID! Please check logfile!"
                                End If
                                MsgBox(msg, MsgBoxStyle.Exclamation)
                            End If
                        End If
                        If clsWD_SET.IndexFile(DOC_PATH, Record_Index, RecordId, _objecttype) = False Then
                            Dim msg = "Die Record-ID konnte nicht gesetzt werden! Bitte überprüfen Sie die Logfile"
                            If USER_LANGUAGE <> "de-DE" Then
                                msg = "Could not index Record-ID! Please check logfile!"
                            End If
                            MsgBox(msg, MsgBoxStyle.Exclamation)
                        Else
                            Dim msg = String.Format("Der Datensatz wurde erfolgreich mit der Datei " & vbNewLine & "{0}" & vbNewLine & "verknüpft!", DOC_PATH)
                            If USER_LANGUAGE <> "de-DE" Then
                                msg = "The record was successfully linked to the file: " & vbNewLine & DOC_PATH
                            End If
                            MsgBox(msg, MsgBoxStyle.Information)
                            ClassHelper.InsertEssential_Log(CURRENT_DOC_ID, "DOC-ID", "FILE LINK CREATED FOR RECORD: " & RecordId.ToString)
                        End If
                    End If
                Next
            Catch ex As Exception
                MsgBox("Unexpected Error in Linking Record to file:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        Else
            MsgBox("Please choose a record!", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
    End Sub

    Private Sub frmWD_Link_to_Record_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        txtRecordId.Text = ""
    End Sub

    Private Sub grvwGrid_RowStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles grvwGrid.RowStyle
        If e.RowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle Then
            e.Appearance.BackColor = Color.Orange
        End If
    End Sub
End Class