C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to populate a TreeView control from an Access database....!


Posted Date: 24 May 2004    Resource Type: Articles    Category: .NET Framework
Author: Prajeesh.Kottarath.SMember Level: Bronze    
Rating: 1 out of 5Points: 10



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

}




Responses

Author: indunil maheshika liyanage    13 Jul 2004Member Level: Bronze   Points : 0
thank you very much. ur answer help me lot



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Synchronization: The key to synchronization is the concept of lock
Previous Resource: Determining operating system version and details
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use