Public Class frmFlujoArbitraje Dim ds As New DataSet Private Operaciones As New Operaciones Private FlujoArbitrajeDAO As New FlujoArbitrajeDAO Dim Todos As String = String.Empty Dim dsOrdenado As New DataSet Dim PosicionBucles As Integer = 0 Public CodigoA As String = String.Empty Sub New() ' Esta llamada es exigida por el diseñador. InitializeComponent() ' Agregue cualquier inicialización después de la llamada a InitializeComponent(). End Sub Sub New(CodigoAsociado As String) Me.New Me.CodigoA = CodigoAsociado End Sub Private Sub frmFlujoArbitraje_Load(sender As Object, e As EventArgs) Handles MyBase.Load OrdenarTablas() AgregarEstructura() End Sub Sub RecorrerOrdenarTablas(ByRef Tabla As DataTable, ByRef Cantidad As Integer, ByRef PosicionTabla As Integer) Dim TablaPadre As New DataTable TablaPadre = Tabla.Copy For Each FilasTabla As DataRow In TablaPadre.Rows Dim Indice As Integer = 0 Dim Index As Integer = PosicionTabla Dim NivelAnterior As Integer = 0 Indice = FilasTabla("Indice") While Index <= Cantidad Dim JIndice As Integer = 0 Dim TablasHijas As DataTable = ds.Tables(Index).Copy For Each JFilasTabla As DataRow In TablasHijas.Rows JIndice = JFilasTabla("NivelAnterior") If JIndice = Indice Then If Not dsOrdenado.Tables.Contains(TablasHijas.TableName) Then PosicionBucles += 1 TablasHijas.Rows.Add(0, 0, "Posicion", PosicionBucles) dsOrdenado.Tables.Add(TablasHijas) RecorrerOrdenarTablas(TablasHijas, Cantidad, Index) PosicionBucles -= 1 End If Exit For End If Next Index += 1 End While Next End Sub Sub OrdenarTablas() ds = FlujoArbitrajeDAO.CargarArbitraje(Me.CodigoA) Dim Index As Integer = 0 Dim CantidadTablas As Integer = ds.Tables.Count - 1 While Index <= CantidadTablas Dim TablaRaiz As New DataTable TablaRaiz = ds.Tables(Index).Copy If Not dsOrdenado.Tables.Contains(TablaRaiz.TableName) Then TablaRaiz.Rows.Add(0, 0, "Posicion", Index) dsOrdenado.Tables.Add(TablaRaiz) RecorrerOrdenarTablas(TablaRaiz, CantidadTablas, Index) End If Index += 1 End While ds.Dispose() ds = New DataSet ds = dsOrdenado End Sub Sub GeneradorFlujo() Dim CantidadTablas As Integer = ds.Tables.Count - 1 Dim Index As Integer = 0 While Index <= CantidadTablas Dim dgvTablas As New DataGridView dgvTablas.DataSource = ds.Tables(Index) flpContenedorTemporal.Controls.Add(dgvTablas) Index += 1 End While End Sub Sub AgregarEstructura() Dim ds As New DataSet ds = Me.ds Dim CantidadTablas As Integer = ds.Tables.Count - 1 Dim Index As Integer = 0 While Index <= CantidadTablas Dim Tabla As New DataTable Tabla = ds.Tables(Index) Dim Codigo As String = String.Empty Dim Reporto As String = String.Empty Dim CodigoTitulo As String = String.Empty Dim NivelAnterior As Integer = 0 For Each Filas As DataRow In Tabla.Rows If Filas("Descripcion") = "Raiz" Then If String.IsNullOrEmpty(Codigo) Then Codigo += " " + Filas("Codigo") Else Codigo += "," + Filas("Codigo") End If Else If Filas("Descripcion") = "Titulo" Then If String.IsNullOrEmpty(CodigoTitulo) Then Codigo += " " + Filas("Codigo") Else Codigo += "," + Filas("Codigo") End If ElseIf Filas("Descripcion") = "Posicion" Then NivelAnterior = Filas("Codigo") * Filas("Codigo") End If End If If Filas("Descripcion").ToString.Trim(" ") = "Reporto" Then Reporto = Filas("Codigo").ToString.Trim(" ") End If If Filas("Descripcion") = "Titulo Generado" Then If String.IsNullOrEmpty(CodigoTitulo) Then CodigoTitulo += " " + Filas("Codigo") Else CodigoTitulo += "," + Filas("Codigo") End If End If Next If Index = 0 Then ObtenerNodosInicio(0, Codigo) ObtenerNodosInicio(0, Reporto) ObtenerNodosInicio(1, CodigoTitulo) Else ObtenerNodosInicio(NivelAnterior + 1, Codigo) ObtenerNodosInicio(NivelAnterior + 2, Reporto) ObtenerNodosInicio(NivelAnterior + 3, CodigoTitulo) End If Index += 1 End While End Sub Sub ObtenerNodos(ByRef Node As TreeNode, ByVal Nivel As Integer, ByRef Valor As String) For Each nodo As TreeNode In Node.Nodes If nodo.Level = Nivel Then If nodo.Parent.LastNode.Text = nodo.Text Then GuardarTrv(Nivel, Valor, nodo) Exit Sub End If Else If nodo.Parent.LastNode.Text = nodo.Text Then ObtenerNodos(nodo, Nivel, Valor) End If End If Next End Sub Sub GuardarTrv(ByVal Nivel As Integer, ByRef Valor As String, ByRef node As TreeNode) Dim Cantidad As Integer = 0 If Not node.Parent Is Nothing Then Cantidad = node.Parent.LastNode.Nodes.Count - 1 End If If Not (Cantidad <= 0) Then node.Nodes.Add(Valor) Else node.Nodes.Add(Valor) End If Valor = String.Empty End Sub Sub ObtenerNodosInicio(ByVal Nivel As Integer, ByVal Valor As String) Dim Cantidad As Integer = trvFlujo.Nodes.Count If Cantidad > 0 Then For Each nodo As TreeNode In trvFlujo.Nodes If nodo.Level = Nivel Then GuardarTrv(Nivel, Valor, nodo) Exit Sub Else ObtenerNodos(nodo, Nivel, Valor) End If Next Else trvFlujo.Nodes.Add(Valor) End If End Sub End Class