Question : how to create a TreeView and Bind its nodes to a DataTable?

Hi,

I am using ASP.NET 2.0

I need to create a TreeView Object in a aspx.cs(code behind) file. I need to bind the TreeView nodes to the DataTable in the code snippet section. I need the TreeView with tree nodes. These nodes should be Index, Name, Year. Also these nodes should have the entries for the Index, Name, Year columns.

Please, show me the code to create the TreeView, Create the tree nodes and bind the nodes to the table in the code snippet section.

Thanks.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Index", System.Type.GetType
                                                     ("System.Int32"));
DataColumn dc2 = new DataColumn("Name", System.Type.GetType
                                                    ("System.String"));
DataColumn dc3 = new DataColumn("Year", System.Type.GetType
                                                    ("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
                
int[] index = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
 
string [] names= {"darren", "peter", "robin", "phil", "wayne", "rob", "doug", "sam"};
 
int[] year = new int[] { 1990, 19991, 1992, 1993, 1994, 1995, 1996, 1997 };
 
DataRow dr;
for (int i = 0; i < index.Length; i++ )
{
   dr = dt.NewRow();
   dr["Index"] = i;
   dr["Name"] = names[i];
   dr["Year"] = year[i];
   dt.Rows.Add(dr);
}

Answer : how to create a TreeView and Bind its nodes to a DataTable?

Check this link. this may help you . it has got sample of Dynamically Populating Nodes in the TreeView with dataset.

http://www.15seconds.com/issue/041117.htm

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
<%@ PageLanguage="C#"%>
 
<%@ ImportNamespace="System.Data"%>
 
<%@ ImportNamespace="System.Data.SqlClient"%>
 
<%@ ImportNamespace="System.Configuration"%>
 
 
 

 
 
 

 

 
    Dynamic Population of the TreeView Control
 
                    
 
void Node_Populate(object sender, 
 
       System.Web.UI.WebControls.TreeNodeEventArgs e)
 
        {
 
            if(e.Node.ChildNodes.Count == 0)
 
            {            
 
                switch( e.Node.Depth )
 
                {  
 
                    case 0:                
 
                        FillAuthors(e.Node);    
 
                        break;            
 
                    case 1:
 
                        FillTitlesForAuthors(e.Node);
 
                        break;        
 
                }    
 
            }            
 
        }
 
 
 
        void FillAuthors(TreeNode node)
 
        {
 
            string connString = System.Configuration.ConfigurationSettings.
 
ConnectionStrings["NorthwindConnnection"].ConnectionString;
 
            SqlConnection connection = new SqlConnection(connString);            
 
            SqlCommand command = new SqlCommand("Select * From 
 
              authors",connection);            
 
            SqlDataAdapter adapter = new SqlDataAdapter(command);
 
            DataSet authors = new DataSet();
 
            adapter.Fill(authors);            
 
            if (authors.Tables.Count > 0)
 
            {
 
                foreach (DataRow row in authors.Tables[0].Rows)
 
                {
 
                    TreeNode newNode = new 
 
                           TreeNode(row["au_fname"].ToString() + " " + 
 
                           row["au_lname"].ToString(), 
 
                           row["au_id"].ToString());
 
                    newNode.PopulateOnDemand = true;
 
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
 
                    node.ChildNodes.Add(newNode);
 
                }
 
            }
 
        }
 
 
 
        void FillTitlesForAuthors(TreeNode node)
 
        {
 
            string authorID = node.Value;
 
            string connString = System.Configuration.ConfigurationSettings.
 
ConnectionStrings["NorthwindConnnection"].ConnectionString;
 
            SqlConnection connection = new SqlConnection(connString);
 
            SqlCommand command = new SqlCommand("Select T.title, 
 
T.title_id From titles T" +
 
" Inner Join titleauthor TA on 
 
T.title_id = TA.title_id " + 
 
                    " Where TA.au_id = '" + authorID + "'", connection);
 
            SqlDataAdapter adapter = new SqlDataAdapter(command);
 
            DataSet titlesForAuthors = new DataSet();
 
            adapter.Fill(titlesForAuthors);
 
            if (titlesForAuthors.Tables.Count > 0)
 
            {
 
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
 
                {
 
                    TreeNode newNode = new TreeNode(
 
row["title"].ToString(), row["title_id"].ToString());
 
                    newNode.PopulateOnDemand = false;
 
                    newNode.SelectAction = TreeNodeSelectAction.None;
 
                    node.ChildNodes.Add(newNode);
 
                }
 
            }
 
        }
 
 
 
    
 

 

 
    
 
    
Random Solutions  
 
programming4us programming4us