Tema: TreeView
Ver Mensaje Individual
  #20  
Antiguo 07-03-2008
eljuanan eljuanan is offline
Miembro
 
Registrado: feb 2008
Posts: 10
Reputación: 0
eljuanan Va por buen camino
Este es el procedimiento, el que tu indicaste pero
modificado para mi consulta:
procedure TFHSFabaMain.CargarTree(Sender: TObject; SQL: String;raiz:integer);
var
I, X, N,C: Integer;
Nod: TTreeNode;
NodSuplementario: TTreeNode;
MiPunteroInteger:^Integer;
q:TDataSet;
color:string;
begin
//Proceso que carga el arbol desde la BD
//Obtenemos los parametros para los colores
e.g('_col1',e.ValorParametro('FAB/NOD/NIV1/COLOR',False));
e.g('_col2',e.ValorParametro('FAB/NOD/NIV2/COLOR',False));
e.g('_col3',e.ValorParametro('FAB/NOD/NIV3/COLOR',False));
e.g('_col4',e.ValorParametro('FAB/NOD/NIV4/COLOR',False));
e.g('_col5',e.ValorParametro('FAB/NOD/NIV5/COLOR',False));
e.g('_col6',e.ValorParametro('FAB/NOD/NIV6/COLOR',False));
e.g('_col7',e.ValorParametro('FAB/NOD/NIV7/COLOR',False));
e.g('_col8',e.ValorParametro('FAB/NOD/NIV8/COLOR',False));
e.g('_col9',e.ValorParametro('FAB/NOD/NIV9/COLOR',False));
e.g('_col10',e.ValorParametro('FAB/NOD/NIV10/COLOR',False));
//Bloqueamos el onChange para que no se ejecute
e.g('_bloqchange',1);
tv.Items.Clear;
// Cargo la consulta a mostrar en el TreeView
q :=e.DataSetFromSQL(self,'select codigo,padre,nombre,nivel from fab_nodos where emp=:e_empresa and dep=:e_departamento order by orden');
q.Close;
q.Open;
if (q.eof) then exit;
//Recorro la consulta registro por registro
C:=0;
q.First;
while not q.eof do
begin
C:=C+1;
q.Next;
end;
q.First;
for X:=0 to (C-1) do
begin
I:=q.fieldByName('Padre').asinteger;
// Si el campo padre vale 0 (no tiene padre)
if I=raiz then
begin //Agrego un nuevo nodo, que nazca directamente del raíz
Nod:=tv.Items.Add (nil,q.FieldByName('nivel').asstring+'.'+q.FieldByName('codigo').asstring+' '+q.FieldByName('nombre').asstring);
Nod.ImageIndex:=-1;
New(MiPunteroInteger);
MiPunteroInteger^:=q.Fieldbyname('Codigo').asinteger;
nod.Data:=MiPunteroInteger;
nod.Selected :=true;
end
else
begin //Agrego un nuevo nodo hijo al padre que le corresponda
//Recorro desde el último nodo al primero, hasta que la propiedad data (donde guardo
//el código de cada nodo, sea igual al campo padre del nodo a agregar
N:=tv.Items.Count -1;
while PunteroInteger(tv.Items[N].Data)^ <>I do
Dec(N);
//Agrego el nodo hijo
nodSuplementario:=tv.Items.AddChild(tv.Items[N],q.FieldByName('nivel').asstring+'.'+q.FieldByName('codigo').asstring+' '+q.FieldByName('nombre').asstring);
nodSuplementario.ImageIndex:=-1;
New(MiPunteroInteger);
MiPunteroInteger^:= q.Fieldbyname('Codigo').asinteger;
nodSuplementario.Data:=MiPunteroInteger;
Nod.selected:=True;
Nod.Expanded:=False;
end;
q.Next;
end;
if (e.ValorParametro('FAB/TV','')=1) then tv.FullExpand;
if (e.ValorParametro('FAB/TV','')=2) then tv.FullCollapse;
e.g('_bloqchange',0);
if (tv.Items.Count>0) then
tvChange(tv,tv.Selected);
end;

Cuando se crea un nuevo nodo en el arbol
el campo orden se inicializa a 100;
como tengo implementada una opcion de copiar y pegar en el arbol
lo que pretendes que cuando copio y pego , el nodo copiado se inserte
inmediatamente antes que el destino en el que lo estoy pegando (orden 99),
pues hasta ahi lo hace bien, incluso si copio y pego alguno mas,
el problema viene al ejecutar el procedure para cargar el arbol, da un error de index, supongo que porque al no estar ordenado de padres a hijoss...
¿se os ocurre algo? Me seria de gran ayuda.
La base de datos es Firebird 2.0
Responder Con Cita