Creating TreeView from SQL database question

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?
Who is Participating?
caner_elciConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.