[Last Call] Learn how to a build a cloud-first strategyRegister Now


Creating TreeView from SQL database question

Posted on 2005-04-16
Medium Priority
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

Accepted Solution

caner_elci earned 1000 total points
ID: 13799412
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

825 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