You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
11 KiB

Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class clsOETree
#Region "Deklarationen"
Dim treedata As New DataSet
Public treedata_for_search As New DataTable
Dim TreeaufbauSuche As Boolean = False
Dim TreeaufbauNr As Integer = 0
Dim FoundNode As Integer = 0
Dim Searchstring As String = ""
Public Treesearch As New TreeView
#End Region
#Region "Tree"
Public Sub Load_Treeview(ByRef Tree As TreeView, ByVal Type As String)
treedata.Tables.Clear()
TreeaufbauNr = -1
Load_Treedata("sp_rpt_" + Type)
If Not (treedata Is Nothing) Then
Dim oView As DataView = treedata.Tables(0).DefaultView
Dim oTable As DataTable = oView.Table
Dim oDS As DataSet = New DataSet()
oDS.Tables.Add(oTable.Copy())
If oDS.Relations.Contains("SelfRefenceRelation") = False Then
oDS.Relations.Add("SelfRefenceRelation", _
oDS.Tables(0).Columns("id"), _
oDS.Tables(0).Columns("Parentid"))
End If
oTable.Dispose()
oTable = Nothing
LoadTreeView(oDS, Tree)
oDS.Dispose()
oDS = Nothing
End If
End Sub
Public Sub Load_Treeview(ByRef tree As TreeView, ByVal per As DateTime)
treedata.Tables.Clear()
TreeaufbauNr = -1
Load_Treedata("Organisation", per)
If Not (treedata Is Nothing) Then
Dim oView As DataView = treedata.Tables(0).DefaultView
Dim oTable As DataTable = oView.Table
Dim oDS As DataSet = New DataSet()
oDS.Tables.Add(oTable.Copy())
If oDS.Relations.Contains("SelfRefenceRelation") = False Then
oDS.Relations.Add("SelfRefenceRelation", _
oDS.Tables(0).Columns("id"), _
oDS.Tables(0).Columns("Parentid"))
End If
oTable.Dispose()
oTable = Nothing
LoadTreeView(oDS, tree)
oDS.Dispose()
oDS = Nothing
End If
End Sub
Private Function Load_Treedata(ByVal datatype As String, ByVal per As DateTime) As DataTable
treedata.Tables.Clear()
Dim selectcommand As New SqlCommand
Dim connection As New SqlConnection()
Dim da As New SqlDataAdapter("", connection)
selectcommand.CommandText = "sp_get_struktur_OE"
'selectcommand.Parameters.Add("@Root", SqlDbType.VarChar)
'selectcommand.Parameters(0).Value = Datatype
selectcommand.CommandType = CommandType.StoredProcedure
selectcommand.Parameters.Add("@per", SqlDbType.DateTime)
selectcommand.Parameters(0).Value = per
selectcommand.Connection = connection
Try
connection.ConnectionString = Globals.sConnectionString
connection.Open()
da.SelectCommand = selectcommand
da.Fill(treedata, "Tree")
If treedata_for_search.Rows.Count < 1 Then
For Each c As DataColumn In treedata.Tables(0).Columns
If c.ColumnName = "ErweiterteSuche" Then
Dim dc As New DataColumn
dc.ColumnName = "ErweiterteSuche"
dc.DataType = System.Type.GetType("System.String")
dc.DefaultValue = " "
Me.treedata_for_search.Columns.Add(dc)
Else
Me.treedata_for_search.Columns.Add(c.ColumnName)
End If
Next
End If
For Each row As DataRow In treedata.Tables(0).Rows
Me.treedata_for_search.ImportRow(row)
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
connection.Close()
da.Dispose()
selectcommand.Dispose()
End Try
End Function
Private Function Load_Treedata(ByVal datatype As String) As DataTable
treedata.Tables.Clear()
Dim selectcommand As New SqlCommand
Dim connection As New SqlConnection()
Dim da As New SqlDataAdapter("", connection)
selectcommand.CommandText = datatype
selectcommand.Connection = connection
Try
connection.ConnectionString = Globals.sConnectionString
connection.Open()
da.SelectCommand = selectcommand
da.Fill(treedata, "Tree")
If treedata_for_search.Rows.Count < 1 Then
For Each c As DataColumn In treedata.Tables(0).Columns
If c.ColumnName = "ErweiterteSuche" Then
Dim dc As New DataColumn
dc.ColumnName = "ErweiterteSuche"
dc.DataType = System.Type.GetType("System.String")
dc.DefaultValue = " "
Me.treedata_for_search.Columns.Add(dc)
Else
Me.treedata_for_search.Columns.Add(c.ColumnName)
End If
Next
End If
For Each row As DataRow In treedata.Tables(0).Rows
Me.treedata_for_search.ImportRow(row)
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
connection.Close()
da.Dispose()
selectcommand.Dispose()
End Try
End Function
''' <summary>
''' Tree aufbauen
''' </summary>
''' <param name="oDS"></param>
''' <param name="oTreeview"></param>
''' <remarks></remarks>
Private Sub LoadTreeView(ByVal oDS As DataSet, ByRef oTreeview As TreeView)
'Dim oTreeView As TreeView = New TreeView()
Dim oDataRow As DataRow
For Each oDataRow In oDS.Tables(0).Rows
If Not oDataRow.IsNull("Parentid") Then
If oDataRow.Item("Parentid") = "0" Then
'If oDataRow.IsNull("Parentid") Then
Dim oNode As New TreeNode()
oNode.Text = Trim(oDataRow("Bezeichnung").ToString() + " (" + oDataRow("id").ToString + ")")
Try
oNode.ToolTipText = Trim(oDataRow("ErweiterteSuche").ToString)
Catch
End Try
oNode.Tag = oDataRow("id").ToString
oNode.ImageIndex = oDataRow("ImageIndex")
oNode.SelectedImageIndex = oDataRow("ImageIndex")
oNode.StateImageIndex = oDataRow("ImageIndex")
oTreeview.Nodes.Add(oNode)
RecursivelyLoadTree(oDataRow, oNode)
End If
End If
Next oDataRow
oDS.Dispose()
oDS = Nothing
End Sub
''' <summary>
''' Child-Nodes hinzufügen
''' </summary>
''' <param name="oDataRow"></param>
''' <param name="oNode"></param>
''' <remarks></remarks>
Private Sub RecursivelyLoadTree(ByVal oDataRow As DataRow, ByRef oNode As TreeNode)
Dim oChildRow As DataRow
For Each oChildRow In oDataRow.GetChildRows("SelfRefenceRelation")
Dim oChildNode As New TreeNode()
oChildNode.Text = Trim(oChildRow("Bezeichnung").ToString() + " (" + oChildRow("id").ToString + ")")
oChildNode.Tag = oChildRow("id").ToString()
oChildNode.ImageIndex = oChildRow("ImageIndex")
oChildNode.SelectedImageIndex = oChildRow("ImageIndex")
oChildNode.StateImageIndex = oChildRow("ImageIndex")
oNode.Nodes.Add(oChildNode)
RecursivelyLoadTree(oChildRow, oChildNode)
Next oChildRow
End Sub
Public Function FindNode(ByVal _nodeCollection As TreeNodeCollection, ByVal SearchVal As String, ByVal SearchString As String) As TreeNode
Dim tmpNode As TreeNode
For Each _child As TreeNode In _nodeCollection
If _child.Tag = SearchVal Then
Return _child
End If
' If InStr(UCase(_child.Text), UCase(SearchString)) > 0 Then
' Return _child
' End If
tmpNode = FindNode(_child.Nodes, SearchVal, SearchString)
If Not tmpNode Is Nothing Then
Return tmpNode
End If
Next
Return Nothing
End Function
Public Function Init_Search()
FoundNode = 0
Searchstring = ""
Me.Treesearch.Nodes.Clear()
End Function
Public Function SearchNode(ByRef tree As TreeView, ByVal SearchString As String) As TreeNode
Me.Treesearch.Nodes.Clear()
For Each dr As DataRow In treedata_for_search.Rows
If InStr(UCase(dr.Item("Bezeichnung")), UCase(SearchString)) > 0 Or InStr(UCase(dr.Item("ErweiterteSuche")), UCase(SearchString)) > 0 Then
Dim tn As New TreeNode
tn = Me.Treesearch.Nodes.Add(dr.Item("Bezeichnung"))
tn.Tag = dr.Item("id")
End If
Next
Return FindFirst(tree)
End Function
Public Function FindFirst(ByRef Tree As TreeView) As TreeNode
Try
If Me.Treesearch.Nodes.Count > 0 Then Me.Treesearch.SelectedNode = Me.Treesearch.Nodes(0)
Return FindNode(Tree.Nodes, Treesearch.SelectedNode.Tag, "")
Catch
End Try
End Function
Public Function FindNextNode(ByRef tree As TreeView) As TreeNode
Try
Treesearch.SelectedNode = Treesearch.SelectedNode.NextNode
If Treesearch.SelectedNode Is Nothing Then
Return FindFirst(tree)
End If
Return Me.FindNode(tree.Nodes, Treesearch.SelectedNode.Tag, "")
Catch ex As Exception
End Try
End Function
Public Function FindPrevNode(ByRef tree As TreeView) As TreeNode
Try
Treesearch.SelectedNode = Treesearch.SelectedNode.PrevNode
If Treesearch.SelectedNode Is Nothing Then
Return FindLastNode(tree)
End If
Return Me.FindNode(tree.Nodes, Treesearch.SelectedNode.Tag, "")
Catch ex As Exception
End Try
End Function
Public Function FindLastNode(ByRef tree As TreeView) As TreeNode
Try
Treesearch.SelectedNode = Treesearch.Nodes(Treesearch.Nodes.Count - 1)
Return Me.FindNode(tree.Nodes, Treesearch.SelectedNode.Tag, "")
Catch ex As Exception
End Try
End Function
#End Region
End Class