Creating TreeView from SQL database question

Posted on 2005-04-16
Last Modified: 2010-04-16
I have a tree view and I want to display customer info that is stored in a database.   I would like to have nodes A through Z in the tree view, and when a user clicks to expand the letter it wll show all of the customers (the custname column) that start wit that letter.  Any ideas on how to accomplish this?
Question by:ike2010
    1 Comment
    LVL 4

    Accepted Solution

    There is no information about loading style of customers. I mean, do you want them to be loaded dynamically when the user expands a letter, or will they be loaded once in a startup method?

    Anyway, I assume that you'll want to load them dynamically, because customer count may be more than thousands. You can do this to load your customers:

    * I assume that there is a global TreeNode named parentNode which has all letter nodes as its children.
    * Also, you'll need to add a dummy TreeNode to each letter node. Just to make them have a plus sign before them, so the user can expand the node.
    * Add BeforeExpand event to your TreeView.
    * You'll notice that there is a Node property in event arguments of this event.
    * Write a code like this in the event handler:

          if( e.Node.Parent == parentNode && e.Node.Nodes.Count == 0 )

    * Write a dynamic loader:

          public void LoadCustomers(TreeNode node)
                string firstLetter = node.Text;
                string sql = "select * from customers where customer_name like '"+firstLetter+"%'";

                node.Nodes.Clear();  // Clear dummy node here

                // execute sql here
                // fetch all rows and create a TreeNode for them, add each customer TreeNode to node.Nodes

                // if there are no rows selected starting with this letter, you'll decide to add a
                // dummy node again to allow user to retry loading customers later (recommended when your
                // application is a multiuser database application), otherwise, you may leave the node this way,
                // without any plus sign before it (without any children), meaning that there are no customers
                // starting with this letter and there will never be.

    I think it gives you an idea.

    Caner ELCI

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now