Web| Program | create | dynamic | design
To create a tree structure with the TreeView control
Database structure Design
This article uses ms-sqlserver2000, which involves the following two database tables:
Table name
Description
Table name
Description
Mainbords
Main Number Table
Subborads
Child tables
The fields in each table are as follows: Field settings in Mainboards
Field
Type
Description
Mainid
Integral type
Index number
Mainname
Character type
The name of the main edition
Field settings in Subboards:
Field settings in Subboards:
Field
Type
Description
SubID
Integral type
Index number
Mainid
Integral type
The corresponding main version number
SubName
Character type
Child name
Field
Type
Description
SubID
Integral type
Index number
Mainid
Integral type
The corresponding main version number
SubName
Character type
Child name
3. 2 Creating a tree structure:
3. 2 Creating a tree structure:
3. 2 Creating a tree structure:
To implement the tree structure, we first have to add the master node, which is the Mainboards table.
To connect to the database first:
SqlConnection CN = new SqlConnection ();
Initialize the connection string, modify it according to your actual situation
CN. connectionstring= "Data source=localhost;initial catalog=bbs;persist security Info=false;user Id=sa; Password=sa; ";
CN. Open ();
SqlDataAdapter ADP = new SqlDataAdapter ("select * from Mainboards", CN);
DataSet ds=new DataSet ();
Adp. Fill (DS);
Add Master Node
int Nummain=ds. Tables[0]. Rows.Count;
for (int i=0;i<nummain;i++)
{
TreeNode node=new TreeNode ();
Node.text =ds. Tables[0]. ROWS[I][1]. ToString ();
Tree. Nodes.Add (Node);
Node.expanded=true;
int Mainid=convert.toint32 (DS2. Tables[0]. Rows[i][0]. ToString ());
Calling the Addsubtree method to add a child node
Addsubtree (Mainid,node);
Addsubtree method
public void Addsubtree (int mainid,treenode pnode)
{//Read the contents of the Subborads table
SqlConnection con = new SqlConnection ();
Con. connectionstring= "Data source=localhost;initial catalog=bbs;persist security Info=false;user Id=sa; Password=sa; ";
Con. Open ();
SqlDataAdapter adp1 = new SqlDataAdapter ("select * from Subboards where mainid= '" "+mainid+" ", con);
DataSet ds1=new DataSet ();
Adp1. Fill (DS1);
int NUMSUB=DS1. Tables[0]. Rows.Count;
To add a child node under a given primary node
for (int i=0;i<numsub;i++)
{
TreeNode node=new TreeNode ();
Node.text =ds1. Tables[0]. ROWS[I][2]. ToString ();
PNODE.NODES.ADD (Node);
After you click here to set the node to redirect to the page:
Node.navigateurl= "display.aspx?id=";
Node.expanded=false;} }