Treeview BIND database

Source: Internet
Author: User
Tags net command

Treeview is a good control. Now we can see that many projects use it. It can implement the operation interface of MMC.

Bytes

Precautions during installation

1. Enter the. NET command and prompt to run the. BAT file.

2. Place the webctrl_client folder in the root directory of the website.

3. Add the reference Microsoft. Web. UI. webcontrols. dll in. net.

This is what I post when an installation error occurs (http://community.csdn.net/Expert/topic/4158/4158536.xml? Temp =. 2670099)

In Microsoft

Http://www.microsoft.com/china/msdn/archives/library/workshop/webcontrols/overview/treeview.asp

The usage of Treeview is comprehensively introduced.

Now let me talk about database binding.

Reference (http://www.microsoft.com/china/community/Column/30.mspx)

1. Create a database

We create a table tbtree in SQL Server 2000. The table structure is designed as follows:
Column name data type description Length Primary Key
Id int node number 4 is
Parentid int parent node No. 4
Context nvarchar: the content of the node to be displayed is 50.

Script for creating a table in SQL Server 2000:

Create Table [DBO]. [tbtree] (
[ID] [int] not null,
[Context] [nvarchar] (50) Collate chinese_prc_ci_as null,
[Parentid] [int] Null
) On [primary]

Add the following records to the table:

Insert tbtree (ID, context, parentid) values (1, 'China', 0)
Insert tbtree (ID, context, parentid) values (2, 'beijing', 11)
Insert tbtree (ID, context, parentid) values (3, 'tianjin ', 11)
Insert tbtree (ID, context, parentid) values (4, 'hebei province ', 1)
Insert tbtree (ID, context, parentid) values (5, 'guangdong province, 1)
Insert tbtree (ID, context, parentid) values (6, 'guangzhou ', 5)
Insert tbtree (ID, context, parentid) values (7, 'sichuan, 1)
Insert tbtree (ID, context, parentid) values (8, 'chengdu ', 7)
Insert tbtree (ID, context, parentid) values (9, 'shenzhen', 5)
Insert tbtree (ID, context, parentid) values (10, 'shijiazhuang ', 4)
Insert tbtree (ID, context, parentid) values (11, 'liaoning province ', 1)
Insert tbtree (ID, context, parentid) values (12, 'dalian ', 11)
Insert tbtree (ID, context, parentid) values (13, 'shanghai', 1)
Insert tbtree (ID, context, parentid) values (14, 'tianhe software key', 6)
Insert tbtree (ID, context, parentid) values (15, 'shantou ', 5)
**************************************** ******************************

Data Binding

Public class index: system. Web. UI. Page
{Dataset DS = new dataset ();
Protected Microsoft. Web. UI. webcontrols. Treeview treeview1;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Sqlconnection Cn = new sqlconnection ();
Try
{
// Initialize the connection string
CN. connectionstring = "Server =.; uid = sa; Pwd = sa; database = test ";
CN. open ();
// Add command to get data from the database
Sqlcommand sqlcmd = new sqlcommand ();
Sqlcmd. Connection = cn;
Sqlcmd. commandtext = "select * From tbtree ";
Sqlcmd. commandtype = commandtype. text;
Sqldataadapter ADP = new sqldataadapter (sqlcmd );
ADP. Fill (DS );
}
Catch (exception ex)
{
Throw (Ex );
}
Finally
{
CN. Close ();
}
// Call a recursive function to generate a Tree Structure
// TN2 = addtree (0, (treenode) null );
// Treeview1.nodes. Add (TN2 );
Addtree (0, (treenode) null );

}
Public void addtree (INT parentid, treenode pnode)
{Microsoft. Web. UI. webcontrols. treenode TN1 = new treenode ();
Dataview dvtree = new dataview (Ds. Tables [0]);
// Filter the parentid to obtain all the current subnodes.
Dvtree. rowfilter = "[parentid] =" + parentid;
Foreach (datarowview row in dvtree)
{
If (pnode = NULL)
{//'? Add Root Node

Tn1.text = row ["context"]. tostring ();
Treeview1.nodes. Add (TN1 );
Tn1.expanded = true;
Addtree (int32.parse (row ["ID"]. tostring (), TN1); // recursive again
}
Else
{// Add the subnode of the current node
Microsoft. Web. UI. webcontrols. treenode TN2 = new treenode ();
Tn2.text = row ["context"]. tostring ();
Pnode. nodes. Add (TN2 );
Tn1.expanded = true;
Addtree (int32.parse (row ["ID"]. tostring (), TN2); // recursive again
}

}
}
 

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.