Construct a Treeview based on the data in the database

Source: Internet
Author: User
The data structure of the database is as follows:
Scompany_no varchar (10) unchecked
Scompany_name varchar (50) unchecked
Sshortname varchar (30) checked
Sparent_no varchar (10) checked
Stree varchar (30) checked
Itreelevel int checked
The specific data is as follows:
000001 Head Office Abbreviation Null 001 1
000002 Company 2 Abbreviation 000001 001001 2
000003 Company 3 Abbreviation 000001 001002 2
000004 Company 4 Abbreviation 000001 001003 2
000005 Company 4 Abbreviation 000001 001004 2
000006 Company 4 subordinates Abbreviation 000005 001004001 3
000007 Company 4 subordinate 2 Abbreviation 000005 001004002 3

Construct the C # Of the Treeview # Code As follows: (implemented using recursive methods) Initialize the Department tree # Region Initialize the Department tree
// Bytes --------------------------------------------------------------------------------------------------------
/**//// <Summary>
///Orm initializes the Department tree
/// </Summary>
/// <Param name = "totreeview">Specify a target Treeview</Param>
Public   Static   Void Companytree_init (Treeview totreeview)
{
Totreeview. nodes. Clear ();
Int Ilevel =   1 ; // Tmpcorp. itreelevel;
Ortcompany Corp =   New Ortcompany ();
Ordatareader < Ortcompany > Reader = Corp. dataaccessor. executereader (commandtype. Text, " Select * From tcompany where sStatus = '1' " , Corp, Null );

Treenode tmpnode =   New Treenode ();
Foreach (Ortcompany u In Reader) // Loop each row of data
{
If (( Int ) U. itreelevel = Ilevel)
{
Treenode rootnode =   New Treenode ();
Rootnode. Text = U. scompany_no.tostring (). Trim () + U. sshortname. tostring (). Trim ();
Rootnode. Value = U. scompany_no.tostring (). Trim ();
Rootnode. tooltip = U. scompany_name;
Totreeview. nodes. Add (rootnode );
Rootnode. Expanded =   True ;
}
Else
{
Tmpnode =   Null ;
For ( Int I =   0 ; I < Totreeview. nodes. Count; I ++ )
{
Treenode ttnode =   New Treenode ();
Ttnode = Findnode (totreeview. nodes [I], U. sparent_no.tostring (). Trim ());
If (Ttnode ! =   Null ) Tmpnode = Ttnode;
}
If (Tmpnode ! =   Null )
{
Treenode subnode =   New Treenode ();
Subnode. Text = U. scompany_no.tostring (). Trim () + U. sshortname. tostring (). Trim ();
Subnode. Value = U. scompany_no.tostring (). Trim ();
Subnode. tooltip = U. scompany_name;
Tmpnode. childnodes. Add (subnode );
Subnode. Expanded =   True ;
}
}
}
Reader. Close ();
}
/**/ ///   <Summary>
/// Recursive search for parent nodes
///   </Summary>
///   <Param name = "tnparent"> Specify a root node and traverse it </Param>
///   <Param name = "strvalue"> Value of the node to be searched </Param>
Public   Static Treenode findnode (treenode tnparent, String Strvalue)
{
If (Tnparent =   Null ) Return   Null ;
If (Tnparent. Value = Strvalue) Return Tnparent;
Treenode tnret =   Null ;
Foreach (Treenode TN In Tnparent. childnodes)
{
Tnret=Findnode (TN, strvalue );
If(Tnret! = Null)Break;
}
Return Tnret;
}
# Endregion

The ORM is used to obtain data. You can use it without it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.