[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 620
  • Last Modified:

Sorting a TreeViewControl

I need to sort a TreeView control based on one of two selected user options. The problem is, that the Text property of my TreeView nodes is set to a string that is actually a cocatenation of two database fields. For example, I am retrieving the followng values from my database: UserID, UserName, and displaying them in my TreeView like this: UserID - UserName.

I need to be able to sort by either of these fields.

I have looked at the TreeViewNodeSorter property, but haven't been able to figure out how to 'tweak' it to meet my needs.

Has anyone here done something similar?

Thanks!!
0
marmaxx
Asked:
marmaxx
  • 3
  • 2
1 Solution
 
2266180Commented:
if I remember correctly, the treeview nodes/items have a property that can hold an object of any type (the type of that property is Object) you can create a wrapper class that will hold the 2 fields and set that treeview node's "object" property. then, in the custom sort event (delegate) you compare using that object from the node casted to the wrapper which will give you the 2 fields.
0
 
marmaxxAuthor Commented:
I believe we're talking about the same property - TreeViewListSorter. I tried your suggestion, but I'm still not having any luck. Would you be willing to post a small snippet, so that I can get a better idea of what you're taling about?

Thanks again for the feedback. I appreciate it.



0
 
2266180Commented:
well .. I only have .net 1.1 here at home, but I'll try to outline the usage:

you will create somthing like this:

            private class RecordWrapper // this holds the strings
            {
                  public string str1;
                  public string str2;
                  public RecordWrapper(string s1, string s2)
                  {
                        str1 = s1;
                        str2 = s2;
                  }
            }

            private class Sorter : IComparer
            {
                  public bool first = true;
                  public int Compare(object x, object y)
                  {
                        TreeNode tx = x as TreeNode;
                        TreeNode ty = y as TreeNode;
                        string s1;
                        string s2;
                        RecordWrapper w1 = tx.Tag as RecordWrapper
                              ,w2 = ty.Tag as RecordWrapper;

                        if (first)
                        {
                              s1 = w1.str1;
                              s2 = w2.str1;
                        }
                        else
                        {
                              s1 = w1.str2;
                              s2 = w2.str2;
                        }
                        if (s1.Length != s2.Length)
                              return s1.Length - s2.Length;

                        return string.Compare(s1, s2);
                  }
            }            

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  TreeNode n;
                  RecordWrapper w;
                  
                  w = new RecordWrapper("abcde","98678");
                  n = new TreeNode();
                  n.Tag = w;
                  treeView1.Nodes.Add(n);// this is how to add it
                  
                  w = new RecordWrapper("mnope","0");
                  n = new TreeNode();
                  n.Tag = w;
                  treeView1.Nodes.Add(n);
                  
                  w = new RecordWrapper("xyhjgj","76");
                  n = new TreeNode();
                  n.Tag = w;
                  treeView1.Nodes.Add(n);
                  
                  w = new RecordWrapper("erjkfsd","1234");
                  n = new TreeNode();
                  n.Tag = w;
                  treeView1.Nodes.Add(n);

                  treeView1.TreeViewNodeSorter = new Sorter();
                  treeView1.TreeViewNodeSorter.first = true;// sort after first record
                  treeView1.TreeViewNodeSorter.first = false;// sort after second record
            }

more info here: http://www.devnewsgroups.net/link.aspx?url=http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.treeviewnodesorter.aspx

since I don't have .net 2.0 I didn't ompile that but it should work.
0
 
marmaxxAuthor Commented:
Thank You!

I may have to tweak it a bit, but I get the idea. This should work for me.

I'm going to go ahead and award the points now since I'm sure I can use this.

I really appreciate you taking the time to provide the snippet.

0
 
2266180Commented:
glad to help :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now