Building a tree directory with the TreeView (C # Edition)

Source: Internet
Author: User
Tags count parent directory tostring trim
TreeView Introduction:

Participated in a large-scale community program development, and now the relevant development experience is summed up, and we discuss. This section mainly discusses the problem of generating a tree-type catalog of infinite classes with the TreeView, and the relevant code extracts from an article in the Community program to publish the Directory Management section of the management system.



First, the database design

Bbs_bestarticlefolder Essence Area Catalogue Table

Key:folderid

Field Name
Type
Length
Default
Description

FolderID
Int
4

ID (AutoNumber)

Parentfolderid
Int
4
0
Parent Directory ID

FolderName
Nvarchar
80
 
Directory Name

Boardid
Int
4
0
Forum ID

Createruserid
Int
4
0
Creator ID

Addtime
Datetime
8
GetDate ()
Creation time


Second, the tree directory construction process

1, install the TreeView control, I think most people will be installed, will not be installed to find related articles under.

2, the concrete realization:

A data generation

Data generation Events #region the tree

Private DataView createdateview (int boardid)

{

Bbsbestarticlefoldercollection bafc=new bbsbestarticlefoldercollection ();

BAFC. Getinfobyparentfolderid (boardid,-1);

int BAFCCOUNT=BAFC. Count;

DataTable dt = new DataTable ("Table");

Dt. Columns.Add ("FolderID", System.Type.GetType ("System.String"));

Dt. Columns.Add ("FolderName", System.Type.GetType ("System.String"));

Dt. Columns.Add ("Parentfolderid", System.Type.GetType ("System.String"));

for (int i=0;i<bafccount;i++)

{

DataRow Dr=dt. NewRow ();

Dr[0]=bafc[i].id;

Dr[1]=bafc[i]. FolderName;

Dr[2]=bafc[i]. Parentfolderid;

Dt. Rows.Add (DR);

}



Dv=dt. DefaultView;

return DV;

}



#endregion

Note: Bbsbestarticlefoldercollection object is a collection class, which stores all eligible data sets, Getinfobyparentfolderid is one of the methods, according to certain conditions to populate the collection class, This article is mainly about the TreeView Building Tree directory, which will be detailed in another article.



B

#region TreeView Node Generation method

private void Createtree (treenodecollection foldertreenodecollection, int parentid)

{

Dv. Rowfilter= "parentfolderid=" +parentid;

int tmpparentfolderid=0;

foreach (DataRowView DRV in DV)

{

TreeNode tn=new TreeNode ();

Tn.id=drv. row["FolderID"]. ToString ();

Tn. Text=drv. row["FolderName"]. ToString (). Trim ();

Tmpparentfolderid=int32.parse (DRV. row["Parentfolderid"]. ToString (). Trim ());

Tn. Checkbox=true;

Foldertreenodecollection.add (TN);



Createtree (Foldertreenodecollection[foldertreenodecollection.count-1]. Nodes,int32.parse (tn.id));

}





}



One explanation: The recursive construction of tree levels of nodes, where DV is the last method generated DataView



c the method of obtaining data has, the method of the birth point is also have, then the rest is to call the



Createtree (treeview1.nodes,0);



One explanation: TreeView1 is the control ID, as you all know, here's 0 is to represent the top-level directory, recursive call down from the top-level directory di.


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.