Skip to main content

Get tree grid elements

Operations on the tree grid are performed using the ITreeGridNode, ITreeGridColumn, and ITreeGridCell objects, which can be obtained from the ITreeGrid object.

Get tree grid rows

An example of getting all rows.

public void GetAllNodes(ICommandContext c, ICommandParams p) 
{
ITreeGrid treeGrid = c.App.Workspace.CurrentEditor as ITreeGrid;
if (treegrid == null) return;

//Get all nodes
var targetNodes = new[] { treegrid.Root };
var allNodes = targetNodes.Union(GetTreeGridNodes(targetNodes));

var no = 1;
foreach(ITreeGridNode node in allNodes)
{
c.App.Output.WriteLine("sample", $"{no}th line : {node.Model.Name}");
no++;
}
}

private IEnumerable<ITreeGridNode> GetTreeGridNodes(IEnumerable<ITreeGridNode> targetNodes)
{
//Recursively collect the child nodes of the target node.
var result = new List<ITreeGridNode>();
foreach (var targetNode in targetNodes)
{
result.Add(targetNode);
result.AddRange(GetTreeGridNodes(targetNode.Children));
}
return result;
}

To get the selected rows, use the GetSelectedNodes method of the ITreeGrid object.

public void GetSelectedNodes(ICommandContext c, ICommandParams p) 
{
ITreeGrid treeGrid = c.App.Workspace.CurrentEditor as ITreeGrid;
if (treeGrid == null) return;

//Get selected nodes
var selectedNodes = treeGrid.GetSelectedNodes();
c.App.Output.WriteLine("sample", $"Number of selected rows: {selectedNodes.Count()}");
}

Get tree grid columns

To get columns, use the Columns property of the ITreeGrid object.

public void GetColumns(ICommandContext c, ICommandParams p) 
{
ITreeGrid treeGrid = c.App.Workspace.CurrentEditor as ITreeGrid;
if (treeGrid == null) return;

//Get columns
var no = 1;
foreach(ITreeGridColumn column in treeGrid.Columns)
{
c.App.Output.WriteLine("sample", $"{no}Column : {column.DisplayName}");
no++;
}
}

Get cell value

To get the cell value of the selected row, use the GetCellValue method of the ITreeGridNode object.

public void GetCellValue(ICommandContext c, ICommandParams p) 
{
ITreeGrid treegrid = c.App.Workspace.CurrentEditor as ITreeGrid;
if (treegrid == null) return;

//Get selected nodes
var selectedNodes = treegrid.GetSelectedNodes();
foreach(ITreeGridNode selectedNode in selectedNodes)
{
object cellValue;

//Get cell value at specified index
cellValue = selectedNode.GetCellValueAt(1);

//Get cell value at specified index as string
cellValue = selectedNode.GetCellValueStringAt(1);

//Get cell value by column
ITreeGridColumn column = ...
cellValue = selectedNode.GetCellValue(column);
cellValue = selectedNode.GetCellValueString(column);

//Get all cell values ​​as strings
var cellValues ​​= selectedNode.GetCellDisplayValues();
}
}

Also, if you want to get only the values ​​of selected cells, you can use the GetSelectedCells method of the ITreeGridNode object.

public void GetSelectedCellValue(ICommandContext c, ICommandParams p) 
{
ITreeGrid treeGrid = c.App.Workspace.CurrentEditor as ITreeGrid;
if (treeGrid == null) return;

//Get selected nodes
var selectedNodes = treeGrid.GetSelectedNodes();
foreach(ITreeGridNode selectedNode in selectedNodes)
{
//Get selected cells
var selectedCells = selectedNode.GetSelectedCells();
foreach(ITreeGridCell selectedCell in selectedCells)
{
//Get cell value
var cellValue = selectedCell.GetValue();
c.App.Output.WriteLine("sample", $"Cell value : {cellValue}");
}
}
}