Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TreeView and DBGrid

Posted on 1999-07-07
6
Medium Priority
?
1,847 Views
Last Modified: 2008-03-17
Hello to all, my question :
I have created a tree view that loads data from sql server , it is drag and drop enabled, i need to know the following
1. How do I make changes permanent on the tree view, for example when i drag a node under another node how do i make these changes permanent, that is i need to delete the data from the underlying table when the node is shifted and add it to a new column. For example dragging $330 from Mr Jones to Mr Pete.
2. Secondly I need to link the tree view to a dbgrid, such that as i move over a Name in the tree view the same column in the dbgrid is highlighted
Thanx very much guys
0
Comment
Question by:niteshs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1386895
hi niteshs,


check out this data-aware treeview component,
good example included
http://www.geocities.com/SiliconValley/Heights/7874/d-dbtv.htm

meikl
0
 

Author Comment

by:niteshs
ID: 1386896
sorry guys but i dont want to install any components, like DBtreeViews
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1386897
you've to handle your code in the OnEndDrag, OnChange etc.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
kretzschmar earned 400 total points
ID: 1386898
preventive lock ;-)
0
 

Author Comment

by:niteshs
ID: 1386899
kretzschmar was excellent with his help!!!
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1386900
hi all,

for your information, and for all who buys this q,

after an intensive email-contact i provided nitesh this sample

unit tree_db_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids, Db, DBTables, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    Table2: TTable;
    procedure Button1Click(Sender: TObject);
    procedure TreeView1Deletion(Sender: TObject; Node: TTreeNode);
    procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Type
  PNodeInfo = ^TNodeInfo;
  TNodeInfo = Record
                UniqueID  : String[30];
                FieldName : String[30];
              end;

var PNI : PNodeInfo;

procedure TForm1.Button1Click(Sender: TObject);
var count1, count2 : integer;
    node, node2 : TTreeNode;
begin
  Treeview1.items.Clear;
  TreeView1.items.BeginUpDate;
  Table1.First;
  While Not(Table1.EOF) do
  begin
    node  := TreeView1.Items.Add(nil,Table1.Fields[1].AsString);
    New(PNI);
    PNI^.UniqueID := Table1.Fields[0].AsString;
    PNI^.FieldName := Table1.Fields[1].FieldName;
    Node.Data := PNI;
    For Count2 := 1 to Table1.FieldCount-1 do
    begin
      Node2 := TreeView1.Items.AddChild(node,Table1.Fields[count2].AsString +' <-- '+
                                       Table1.Fields[count2].FieldName);
      New(PNI);
      PNI^.UniqueID := Table1.Fields[0].AsString;
      PNI^.FieldName := Table1.Fields[Count2].FieldName;
      Node2.Data := PNI;
    end;
    table1.Next;
  end;
  Treeview1.Items.EndUpDate;
end;

procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
  PNI := Node.Data;
  if PNI <> Nil then
    Dispose(PNI);
  Node.Data := Nil;
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var Node : TTreeNode;
begin
  accept := False;   //First do not accept
  if sender = Source then  //Same Control ?
  begin
    Node := TreeView1.GetNodeAt(X,Y);
    If Node <> Nil then    //Over a Node ?
      If Node <> TreeView1.Selected then  //Not the Same Node ?
        If (Node.Parent = Nil) and (TreeView1.Selected.Parent = Nil) then  //Both Root Node ?
          Accept := True
        else
          If (Node.Parent = Nil) and Not(Node.Expanded) then  //Expand the Root, if a Field dragged
            Node.Expand(False)
          else
            If PNodeInfo(Node.Data)^.FieldName =
               PNodeInfo(TreeView1.Selected.Data)^.FieldName then  //Same Field ?
              Accept := True;
  end;
end;

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  Node, Node2 : TTreeNode;
  S : String;
  I : Integer;
begin
  Node := TreeView1.GetNodeAt(X,Y);
  Table2.Locate('ID',PNodeInfo(TreeView1.Selected.Data)^.UniqueID,[]);  //Synchronize Tables
  Table1.Locate('ID',PNodeInfo(Node.Data)^.UniqueID,[]);
  if Node.Parent = Nil then  //Apply to whole Record
  begin
    Case RadioGroup1.ItemIndex of
      0 : Begin              //Copy
            Table1.Edit;     //First apply to Table
            For I := 1 to Table1.FieldCount - 1 do
              Table1.Fields[I].Assign(Table2.Fields[I]);
            Table1.Post;
            Node.Text := Table1.Fields[1].AsString;  //Second Apply to Tree
            Node2 := Node.GetFirstChild;
            while node2 <> Nil do
            begin
              node2.Text := Table1.FieldbyName(PNodeInfo(Node2.Data)^.FieldName).AsString +
                    ' <-- '+Table1.FieldbyName(PNodeInfo(Node2.Data)^.FieldName).FieldName;
              Node2 := Node.GetNextChild(Node2);
            end;
          end;
      1 : Begin   //Swap
            Table1.Edit;
            Table2.Edit;
            For I := 1 to Table1.FieldCount - 1 do
            begin
              S := Table1.Fields[I].AsString;
              Table1.Fields[I].Assign(Table2.Fields[I]);
              Table2.Fields[I].Value := S;
            end;
            Table1.Post;
            Table2.Post;
            Table1.Refresh;
            Node.Text := Table1.Fields[1].AsString;
            Node2 := Node.GetFirstChild;
            while node2 <> Nil do
            begin
              node2.Text := Table1.FieldbyName(PNodeInfo(Node2.Data)^.FieldName).AsString +
                    ' <-- '+Table1.FieldbyName(PNodeInfo(Node2.Data)^.FieldName).FieldName;
              Node2 := Node.GetNextChild(Node2);
            end;
            Node := Treeview1.Selected;
            Node.Text := Table2.Fields[1].AsString;
            Node2 := Node.GetFirstChild;
            while node2 <> Nil do
            begin
              node2.Text := Table2.FieldbyName(PNodeInfo(Node2.Data)^.FieldName).AsString +
                    ' <-- '+Table2.FieldbyName(PNodeInfo(Node2.Data)^.FieldName).FieldName;
              Node2 := Node.GetNextChild(Node2);
            end;
          end;
    end;
  end
  else
  begin  //Apply One Field
    Case RadioGroup1.ItemIndex of
      0 : Begin              //Copy
            Table1.Edit;
            Table1.FieldByName(PNodeInfo(Node.Data)^.FieldName).Assign(Table2.FieldByName(PNodeInfo(Node.Data)^.FieldName));
            Table1.Post;
            node.Text := Table1.FieldbyName(PNodeInfo(Node.Data)^.FieldName).AsString +
                   ' <-- '+Table1.FieldbyName(PNodeInfo(Node.Data)^.FieldName).FieldName;
          end;
      1 : Begin
            Table1.Edit;
            Table2.Edit;
            S := Table1.FieldByName(PNodeInfo(Node.Data)^.FieldName).AsString;
            Table1.FieldByName(PNodeInfo(Node.Data)^.FieldName).Assign(Table2.FieldByName(PNodeInfo(Node.Data)^.FieldName));
            Table2.FieldByName(PNodeInfo(Node.Data)^.FieldName).Value := S;
            Table1.Post;
            Table2.Post;
            node.Text := Table1.FieldbyName(PNodeInfo(Node.Data)^.FieldName).AsString +
                   ' <-- '+Table1.FieldbyName(PNodeInfo(Node.Data)^.FieldName).FieldName;
            Node := Treeview1.Selected;
            node.Text := Table2.FieldbyName(PNodeInfo(Node.Data)^.FieldName).AsString +
                   ' <-- '+Table2.FieldbyName(PNodeInfo(Node.Data)^.FieldName).FieldName;

          end;
    end;
  end;
end;

procedure TForm1.TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var Node : TTreeNode;
begin
  Node := TreeView1.GetNodeAt(X,Y);
  If Node <> Nil then
    Table1.Locate('ID',PNodeInfo(Node.Data)^.UniqueID,[]);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Table1.DatabaseName := ExtractFilePath(Application.ExeName);
  Table2.DatabaseName := ExtractFilePath(Application.ExeName);
  Table1.Open;
  Table2.Open;
end;

end.

meikl
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question