unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, NxGridView6, NxColumns6,
  NxControls6, NxCustomGrid6, NxVirtualGrid6, NxGrid6, NxCells6;

type
  TForm2 = class(TForm)
    NextGrid61: TNextGrid6;
    NxTreeColumn61: TNxTreeColumn6;
    NxReportGridView61: TNxReportGridView6;
    Button1: TButton;
    NxTextColumn61: TNxTextColumn6;
    NxTextColumn62: TNxTextColumn6;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure NextGrid61MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure NextGrid61DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure NextGrid61DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    FDraggingIndex: Integer;
    FDraggingRow: INxRow;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  NextGrid61.AddCells(['MASTER', '0', '1'], [0, 1, 2]);
  for i := 1 to 3 do
    with NextGrid61.AddChildRow(0, 1) do
      cells.Item[0].AsString := 'Child ' + inttostr(i);
  for i := 1 to 3 do
    with NextGrid61.AddChildRow(2, 1) do
      cells.Item[0].AsString := 'Sub-Child ' + inttostr(i);
  for i := 1 to 3 do
    with NextGrid61.AddChildRow(6, 1) do
      cells.Item[0].AsString := 'Sub-baby ' + inttostr(i);
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  b: Boolean;
begin
  b := NextGrid61.Row[0].IsParentOf(NextGrid61.Row[4]);
  Caption := BoolToStr(b, True);

  b := NextGrid61.Row[0].IsDescendant(NextGrid61.Row[4]);
  Caption := Caption + ' | ' + BoolToStr(b, True);
end;

procedure TForm2.NextGrid61DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  RowIndex: Integer;
  NewParent: INxRow;
begin
  RowIndex := NextGrid61.ActiveView.GetCellAtPos(Point(X, Y)).Y;
  NewParent := nil;
  if (RowIndex >= 0) and (RowIndex < NextGrid61.RowCount) then
  begin
    NewParent := NextGrid61.Row[RowIndex];
    NextGrid61.Row[FDraggingIndex].MoveTo(NewParent);
    FDraggingIndex := -1;
    NextGrid61.Invalidate;
  end;
end;

procedure TForm2.NextGrid61DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  RowIndex: Integer;
begin
  RowIndex := NextGrid61.ActiveView.GetCellAtPos(Point(X, Y)).Y;
  Accept := (RowIndex >= 0) and (RowIndex < NextGrid61.RowCount)
    and (RowIndex <> FDraggingIndex)
    and not FDraggingRow.IsParentOf(NextGrid61.Row[RowIndex]);
end;

procedure TForm2.NextGrid61MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  RowIndex: Integer;
begin
  RowIndex := NextGrid61.ActiveView.GetCellAtPos(Point(X, Y)).Y;
  if (RowIndex >= 0) and (RowIndex < NextGrid61.RowCount) then
  begin
    FDraggingIndex := RowIndex;
    FDraggingRow := NextGrid61.Row[FDraggingIndex];
    NextGrid61.BeginDrag(False, 4);
  end;
end;

end.
