|
MSSQL 테이블 입니다.
테이블구조는
no = int
parent_no = int
text = char
parent_no 가 0 은 root를 의미합니다.
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ADODB.hpp>
#include <ComCtrls.hpp>
#include <Db.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TTreeView *tv;
TADOQuery *q;
TButton *Button2;
TButton *Button3;
TButton *Button4;
TEdit *Edit1;
TButton *Button5;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private: // User declarations
TStringList*plist;
TList*nlist;
void __fastcall TForm1::load_all();
void __fastcall TForm1::load_sub(TTreeNode*p_node,AnsiString p_no);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
class node_data
{
public:
AnsiString p_no;
AnsiString no;
node_data(AnsiString p,AnsiString n){p_no=p;no=n;}
~node_data(){}
};
void __fastcall TForm1::load_all()
{
plist = new TStringList();
nlist = new TList();
tv->Items->Clear();
load_sub(0,"0"); // 0 is root
while(plist->Count)
{
AnsiString p_no=plist->Strings[plist->Count-1];
TTreeNode*tn=(TTreeNode*)(nlist->Items[nlist->Count-1]);
plist->Delete(plist->Count-1);
nlist->Delete(nlist->Count-1);
load_sub(tn,p_no);
}
delete plist;
delete nlist;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::load_sub(TTreeNode*p_node,AnsiString p_no)
{
q->SQL->Clear();
q->SQL->Add("select * from tree where parent_no="+p_no);
q->Open();
while(!q->Eof)
{
// add to node
TTreeNode*tn=tv->Items->AddChild(p_node,q->FieldByName("text")->AsString);
if(p_node){
tn->Data=new node_data(((node_data*)p_node->Data)->no,q->FieldByName("no")->AsString);
}else{
tn->Data=new node_data("0",q->FieldByName("no")->AsString);
}
// add to parent list
plist->Add(q->FieldByName("no")->AsString);
// add to parent node list
nlist->Add(tn);
q->Next();
}
q->Close();
}
void __fastcall TForm1::Button2Click(TObject *Sender) //루트노드 추가
{
q->SQL->Clear();
q->SQL->Add("insert into tree (parent_no,text) values (0,'"+Edit1->Text.Trim()+"')");
q->ExecSQL();
// if success
q->SQL->Clear();
q->SQL->Add("select @@identity as no");
q->Open();
TTreeNode*tn=tv->Items->AddChild(0,Edit1->Text.Trim());
tn->Data=new node_data(0,q->FieldByName("no")->AsString);
q->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender) //load tree
load_all();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender) //차일드 노트 추가
{
if(!tv->Selected){
ShowMessage("select parent node!!!");
return;
}
q->SQL->Clear();
q->SQL->Add("insert into tree (parent_no,text) values ("+((node_data*)tv->Selected->Data)->no+",'"+Edit1->Text.Trim()+"')");
q->ExecSQL();
// if success
q->SQL->Clear();
q->SQL->Add("select @@identity as no");
q->Open();
TTreeNode*tn=tv->Items->AddChild(tv->Selected,Edit1->Text.Trim());
tn->Data=new node_data(((node_data*)tv->Selected->Data)->p_no,q->FieldByName("no")->AsString);
q->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender) //노드삭제
{
if(!tv->Selected){
ShowMessage("select node!!!");
return;
}
if(tv->Selected->Count){
ShowMessage("select last node!!!");
return;
}
q->SQL->Clear();
q->SQL->Add("delete from tree where no="+((node_data*)tv->Selected->Data)->no);
q->ExecSQL();
// if success
delete tv->Selected->Data;
tv->Selected->Delete();
}
//---------------------------------------------------------------------------
.끝.
이득재 님이 쓰신 글 :
: 안녕하세요.. 항상 이곳에서 많은 도움을 얻고 있습니다.
: 오늘도 염치없게두.. 도움을 얻고자 합니다.
:
: 제가 하고 싶은 건???
:
: ////////////////////////////////////////////////////
: TreeView의 각 노드와 DB와 연결하는 것입니다.
: ////////////////////////////////////////////////////
:
: 물론, 각 TreeView노드가 고정되어 있다면, 바로 연결이 가능하겠지만...
: TreeView가 "추가/수정/삭제"가 된다면... 어떻게 해줘야 할지 막막합니다..
:
: 그래서, 이렇게 구현도 해봤습니다.
: Treeview가 생성될때, 최상위 노드까지의 경로을 알아내서, 경로를 저장 --> 검색성공(엄청난 노가대)..
: ㅠㅠ';;; 물론, TreeView의 구조는 savetofile과 loadfromfie로 따로 저장했습니다.
:
: 하지만, 문제가 있었습니다.. Client가 Explore라면...
: Treeview구조을 알아낼.. 길이 없습니다....
:
: 결론은???
: DB에 모든 TreeView의 정보을 저장해서... 추가/생성/삭제가 되면서....
: 검색이 진행이 된다면.. 어떻게 될것도 같은데... ㅠㅠ;;;
: 에궁.. 넘.. 힘들다.. 그리고, 이게 맞는 방법인지도 모르겠고.. .@.@ ;;;;
: 부디.... 부디... 답변을 주시기를.. 간절히.. 애절히... 애타게...
: 기다립니다.... TT;;;
:
: 에궁 4개월차 허접 프로그램 올림...
:
: 참고, 아무리.. 조그만 정보라도 제게는 무진장 큰... 도움이 된답니다...
: 그리고, 도움 주시는 분에게 꼭~~~~ 나중에 만나면.. 밥 사드릴께여.. 진짜루여...
|