The Treeview here conatins two levels of nodes. The first level (Rootnode) specifies the database name and the second level ( childnodes) specifies the records from a specified field from the selected database!The nodes are identified with the help of images (icons)..ie, One icon for rootnode, another icon for child nodes and one foe selected nodes. How stuff works.....:-
1. Select the TreeView control or add one to the form. 2. Select the ImageList component, or add one to the form. 3. In the Properties window, click the ellipsis button () next to the Images property. The Image Collection Editor appears. 4. Use the Add and Remove buttons to add and remove images from the list. 5. In the TreeView property set the image List,and 6. Select the ImageIndex to 1, and 6. Select the SelectedImageIndex to 2
Now back to Code....
// Form Load Event private OleDbDataReader myReader;
private void TreeView_Load(object sender, System.EventArgs e) { treeView1.Nodes.Clear(); //Clears the nodes everytime the form loads string query = "SELECT Serial_No,Description FROM TreeItems WHERE NodeLevel = 0"; OleDbCommand TreeCommand = new OleDbCommand(query, OledbConnection1); TreeCommand.Connection.Open(); //To open the database connection
//Adds data to the datareader myReader; myReader = TreeCommand.ExecuteReader(CommandBehavior.CloseConnection); string dbName = OledbConnection1.DataSource.ToString(); //Name of the database treeView1.Nodes.Add(dbName); //Add DName as the root node treeView1.TopNode.ImageIndex = 0; //Adds the image treeView1.TopNode.SelectedImageIndex = 0; string rootName = dbName;
while(myReader.Read()) //Add Nodes to the tree from DataReader { TreeNode node = new TreeNode(); //Represents a node of a TreeView. node.Tag = myReader.GetValue(0).ToString(); //Adds SerialNo to the tag node.Text = myReader.GetString(1);
treeView1.TopNode.Nodes.Add(node); treeView1.ExpandAll(); } myReader.Close(); //Closes the datareader TreeCommand.Connection.Close(); //Closes the connection }
|
| Author: indunil maheshika liyanage 13 Jul 2004 | Member Level: Bronze Points : 0 |
thank you very much. ur answer help me lot
|