1

I'm pulling a column of information from a database thru odbc. The column will have numbers in it ranging from 1 to 9999999. I want to be able to organize the values into a treeview with up to 3 levels (parent, child, and grandchild) in the following way:

ideal structure would be: AAABBCC
parent: AAA0000
child: AAABB00
grandchild: AAABBCC

however if the level above doesn't exist then the value moves up in a level. For example, assuming that the data contains the following {1, 101, 200, 204, 1200, 1205, 1304, 290000, 291500, 291502,410204}

-- 1
-- 101
-- 200
------- 204
--1200
-------1205
--1304
--290000
--------291500
----------------291502
--410204

Any help would be greatly appreciated.
Mark

3
  • 2
    winforms, wpf, silverlight, wp7, asp.net web forms, or mvc, or other? Commented Apr 20, 2012 at 19:08
  • 1
    C# is a language, not a ui framework. Commented Apr 20, 2012 at 21:34
  • Sorry, i'm using windows forms Commented Apr 24, 2012 at 15:45

1 Answer 1

1

This is really a very simple problem.

  1. Convert your list of numbers to strings by calling .ToString("D7") on each number.
  2. Sort the converted list.
  3. Take the SubString(0,3) to get your parent value.
  4. Take the SubString(3,2) to get your child value.
  5. Take the Substring(5,2) to get your grandchild value.
  6. If your child value is 00, create a new TreeNode parentNode and add it to the TreeNodeCollection.
  7. If your child value is not 00 and your grandchild value is 00, create a new TreeNode childNode and add it to parentNode.
  8. If your grandchild value is not 00, create a new TreeNode grandchildNode and add it to your childNode.

If you need to fill gaps in sequence, a little extra logic is required but it's fairly trivial.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.