Features / TreeGrid / Node Operations
Node Operations

TreeGrid not only allows you to present the data in a structured form but it also supports performing various operations with the nodes. You can add, delete, expand, and collapse nodes by using various methods provided by the FlexGrid (or C1FlexGrid) class in bound and Unbound Mode.

TreeGrid Operations

You can add a new node to the TreeGrid using the Add method of the Collection class. This method appends a new node row to the collection. The code below shows how to add a node in TreeGrid at the end on the Click event of a button control:

Add a Node

You can add a new node to the TreeGrid using the Add method of the Collection class. This method appends a new node row to the collection. The code below shows how to add a node in TreeGrid at the end on the Click event of a button control:

CS
Copy Code
private void btnBoundAdd_Click(object sender, RoutedEventArgs e)
{  
    var pn = flexGridBound.ItemsSource as ObservableCollection<CustomerInfo>;
    var newcn = new CustomerInfo()
    {
        parentName = "CustomerID 1003",
        childName = new ObservableCollection<CustomerInfo>()
        {
          new CustomerInfo
                {
                    parentName = "Customer Details",
                    childName = new ObservableCollection<CustomerInfo>
                    {
                        new CustomerInfo { parentName = "First Name: James"},
                       new CustomerInfo { parentName = "Last Name:  Anderson"},
                       new CustomerInfo { parentName = "E-mail: j.anderson@mail.com"}
                    }
                },
          new CustomerInfo
          {
                    parentName = "Order Details",
                    childName = new ObservableCollection<CustomerInfo>
                    {
                        new CustomerInfo { parentName = "Product Name: Purse"},
                        new CustomerInfo { parentName = "Quantity: 1"},
                        new CustomerInfo { parentName = "Order Date: 28-5-2024"},
                        new CustomerInfo { parentName = "Price: $60" }
                    }
          }
        }
    };

    pn.Add(newcn);            
}

Delete a Node

You can delete a selected node from the TreeGrid using the RemoveAt method of the Collection class. Following code demonstrates how to delete a node from the TreeGrid on the Click event of a button control:

CS
Copy Code
private void btnBoundDelete_Click(object sender, RoutedEventArgs e)
{   
    var pn = flexGridBound.ItemsSource as ObservableCollection<CustomerInfo>;
    if (pn.Count > 0)
    {
        pn.RemoveAt(pn.Count - 1);
    }           
}

Expand and Collapse Nodes

You can expand and collapse all nodes in your Tree Grid application using the IsCollapsed property of the GridGroupRow class. This feature makes it convenient to navigate through node headers as a group when required. Following code shows how to expand and collapse nodes in TreeGrid on the Click event of a button control.

CS
Copy Code
private void btnBoundExpand_Click(object sender, RoutedEventArgs e)
{
    if (flexGridBound.Selection is null) return;
    int row = flexGridBound.Selection.Row;
    if (flexGridBound.Rows.Count > 0 && row >= 0 && flexGridBound.Rows[row] is GridGroupRow groupRow)
    {
        groupRow.IsCollapsed = false;                
    }
}        
private void btnBoundCollapse_Click(object sender, RoutedEventArgs e)
{
    if (flexGridBound.Selection is null) return;
    int row = flexGridBound.Selection.Row;
    if (flexGridBound.Rows.Count > 0 && row >= 0 && flexGridBound.Rows[row] is GridGroupRow groupRow)
    {
        groupRow.IsCollapsed = true;                
    }
}
You can use the CollapseGroups and CollapseGroupsToLevel methods to either collapse the whole tree or collapse the nodes to a certain level in .NET and .NET Framework respectively.

Sort and Filter Nodes

The TreeGrid includes sorting and filtering capabilities for efficiently organizing data and searching the relevant information. You can easily sort and filter data at runtime in a tree grid using the column header menu.

When sorting the TreeGrid. it ensures that not only the parent nodes are organized but also the child nodes follow the same order while maintaining their parent-child relationships. This keeps the hierarchical structure intact.

tree sort

With filtering in the TreeGrid, an item remains visible if either the item itself or any of its children match the filter criteria. This not only preserves the hierarchical integrity of the data but also improves user experience by ensuring that relevant items are easily accessible.

tree filter

In other words, both sorting and filtering operations provide a comprehensive view of the data.