Search code examples
.netinfragisticsultratree

How do I reorder nodes in an UltraTree?


I've got a class that looks like this:

public class GeneralStatusInfo
{        
    public List<string> List_BLNumber { get; set; }
    public List<POInfo> List_PONumbers { get; set; }
    public List<string> List_Pickup { get; set; }
    public List<string> List_Origin { get; set; }
    public List<string> List_Destination { get; set; }
    public List<string> List_NotifyName { get; set; }
    public List<AppmntInformation> List_Appointments { get; set; }
}

When I bind the data like this:

List<GeneralStatusInfo> statusBind = new List<GeneralStatusInfo>();
statusBind.Add(status);
utGeneralStatusInfo.DataSource = statusBind;

SetupTree(status);

It puts my parent nodes in a different order:

 Appointment 
 P/O Number 
 B/L Number 
 Origin 
 Pickup 
 Notify 
 Payment
 Destination

How do I reorder the nodes so they appear in the same order that I have them in my class?


Solution

  • You have to make your own IComparer. Something like this:

    public class  GeneralStatusInfoMemberOrderComparer: IComparer
    {
        public  GeneralStatusInfoMemberOrderComparer()
        {
            memberOrdermemberOrder.Add("B/L Number",0);
            memberOrdermemberOrder.Add("P/O Number",1);
            /// 
            /// add more items
            ///
        }
    
        private bool sortAlphabetically=false;
        private Dictionary<string,int> memberOrder  = new Dictionary<string,int>();
    
        public bool SortAlphabetically
        {
            get{return sortAlphabetically;}
            set{sortAlphabetically = value;}
        }
    
        int IComparer.Compare(object x, object y)
        {
            string propertyX = x as string;
            string propertyY = y as string;
    
            if (sortAlphabetically)
            {
                return propertyX.CompareTo(propertyY);
            }
            else
            {
                int orderX= memberOrder.ContainsKey(propertyX) ? memberOrder[propertyX] : -1;
                int orderY= memberOrder.ContainsKey(propertyY) ? memberOrder[propertyY] : -1;
                return orderX.CompareTo(orderY);
            }
        }
    }
    

    And then just set SortComparer property of your UltraTree instance:

    ultraTree1.SortComparer = new GeneralStatusInfoMemberOrderComparer();