Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView and Database

Status
Not open for further replies.

cmsamcfe

Programmer
Dec 18, 2002
31
0
0
GB
Following earlier posts, I have written some code to take records from a database and populate a treeview from those records.

However, the code is not working 100% correctly and I am having trouble pinning down the problem


I have included 2 tree diagrams - TREE OUT shows the tree my code currently generates. CORRECT TREE shows the tree as it should be generated.

I have included the code that generates the tree at the bottom of this message.

Can anyone spot the reason for the extra incorrect nodes in my tree?

Thanks

------------------------------------------------------------

TREE OUT:

REFERENCES
C:
+ATRI
| +IMGS
| | +F16
| | F16_000
| | F16_001
| | +F16
| | F16_002
| +IMG
| | +FOOTBALL
| | OWEN_001
| +ATRI
| | +IMG
| | +FOOTBALL
| | | OWEN_002
| | | RVN_001
| | | +FOOTBALL
| | | BECKS_001
| | +CARS
| | | FIESTA_001
| | +IMG
| | +CARS
| | FIESTA_002
| +ATRI
| +ATRI
| +ATRI
| +ATRI
|
+IMAGES
| TEST_001
+C:
+TEMP
TEST_002


-----------------------------------------------------------
CORRECT TREE:

REFERENCES
C:
+ATRI
| +IMGS
| | +F16
| | F16_000
| | F16_001
| | F16_002
| +IMG
| +FOOTBALL
| | OWEN_001
| | OWEN_002
| | RVN_001
| | BECKS_001
| +CARS
| FIESTA_001
| FIESTA_002
|
+IMAGES
| TEST_001
|
+TEMP
TEST_002



------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

TTreeNode *rootNode,
*parentNode,
*childNode;

int recCount=0,
strCmpRes=0;
char *ptr;

AnsiString branchData;

imgTable->First(); // move to first record of table
TreeView1->Items->Clear();
rootNode = TreeView1->Items->Add(NULL,"references");
parentNode = rootNode;

for (recCount = 0; recCount < imgTable->RecordCount; recCount++)
{

/*
Get each &quot;line&quot; of data from the database, storing it in an edit box
Split that line into substrings using strtok

for all split strings
check parent node for children
if no children
add the current string as a child
set the new child as the parent
else
for all child nodes
compare string with child node name
if the string matches (i.e. node exists)
make the child node the new parent
else
insert the string as a new child node of the current parent

reset the parent node to root
move to the next record (to get the next string)
*/

// Get info from database fields
branchData = imgTable->FieldByName(&quot;img_directory&quot;)->AsString.UpperCase()+imgTable->FieldByName(&quot;img_filename&quot;)->AsString.UpperCase();

ptr = strtok(branchData.c_str(),&quot;\\&quot;);
while(ptr!=NULL)
{

if (!parentNode->HasChildren) // if parent has no children
{
TreeView1->Items->AddChild(parentNode, ptr);
// set the new child as the parent
parentNode = parentNode->getFirstChild();
}
else // parent has child nodes
{
// for all child nodes
for (int j = 0; j < parentNode->Count; j++)
{
strCmpRes = AnsiCompareStr(ptr,parentNode->Item[j]->Text);
if (strCmpRes ==0)
parentNode = parentNode->Item[j];
else
{
TreeView1->Items->AddChild(parentNode,ptr);
} // end duplicate else
} // end child for loop

} // end parent has child else

ptr = strtok(NULL,&quot;\\&quot;);

} // end ptr!NULL while loop

// need to reset the parent node to the beginning. At the end of this loop it is set to F16_000
parentNode = rootNode;
// Move to next record
imgTable->Next();

} // end of loop through records

-----------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top