I have tried every solution mentioned on these forums, with multiple variations, and the active node is still not highlighted (with background color) when the tree is loaded.
I set the tree data source to a dataset and expanded everything, this all works great.
I then tried several variations of the below, but the node is never highlighted.
var node = ultraTree1.GetNodeByKey("5"); ultraTree1.SelectedNodes.Clear(); node.BringIntoView(); ultraTree1.ActiveNode = node; ultraTree1.HideSelection = false; node.Selected = true;
The active node is correct and loads my list view with the related data. Even trying to override the back color didn't work. When I manually select the node, it highlights properly.
Hello Sam,
With the default implementation of the UltraTree, the tree needs to be the focused control to be able to see the active / selected node. You can override this by setting the following though:
this.ultraTree1.Override.ActiveNodeAppearance.BackColor = SystemColors.Highlight; this.ultraTree1.Override.ActiveNodeAppearance.ForeColor = Color.White;
Please let me know if you have any other questions or concerns on this matter.
Thanks for the response, but that did not work. Is there an execution order that matters? I tried various ones but nothing changes.
Is there a way to set the tree to the focused control on the form? I tried ultraTree1.Focus() but that did not help.
I tried attaching a zip file here but it kept giving me an error. The code in my test class is below and the designer file.
using Infragistics.Win.UltraWinTree; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; namespace UltraTree_DataBinding_CS { public partial class DataTreeTest : Form { DataSet ds = new DataSet(); string oldNodeText; ToolStripMenuItem menuMove = new ToolStripMenuItem { Name = "MoveTo", Text = "Move To" }; [STAThread] static void Main() { //Load the IG isl File Infragistics.StyleLibrary.GetStyleLibrary style = new Infragistics.StyleLibrary.GetStyleLibrary(); string islFile = style.GetIsl(); Infragistics.Win.AppStyling.StyleManager.Load(islFile); Application.Run(new DataTreeTest()); } public DataTreeTest() { InitializeComponent(); LoadData(); ultraTree1.Focus(); } private void LoadData() { DataTable folders = new DataTable("Folders"); folders.Columns.Add("ID", typeof(int)); folders.Columns["ID"].AutoIncrement = true; folders.Columns["ID"].AutoIncrementSeed = 1; folders.Columns["ID"].AutoIncrementStep = 1; folders.Columns.Add("Name", typeof(string)); folders.Columns.Add("PID", typeof(int)); folders.Rows.Add(0, "Attachments", null); folders.Rows.Add(1, "A", 0); folders.Rows.Add(2, "B", 0); folders.Rows.Add(3, "C", 2); folders.Rows.Add(4, "D", 3); folders.Rows.Add(5, "E", 1); folders.Rows.Add(6, "F", 5); folders.Rows.Add(7, "G", 6); folders.Rows.Add(8, "H", 0); folders.Rows.Add(9, "I", 0); ds.Tables.Add(folders); GenerateFileMoveMenu(); DataTable files = new DataTable("Files"); files.Columns.Add("FileID", typeof(int)); files.Columns.Add("Name", typeof(string)); files.Columns.Add("FolderID", typeof(int)); files.Columns["FolderID"].ReadOnly = false; files.Rows.Add(1, "File 1", 1); files.Rows.Add(2, "File 2", 1); files.Rows.Add(3, "File 3", 3); files.Rows.Add(4, "File 4", 4); files.Rows.Add(5, "File 5", 5); files.Rows.Add(6, "File 6", 5); files.Rows.Add(7, "File 7", 1); ds.Tables.Add(files); DataRelation dr = new DataRelation("RR", folders.Columns["ID"], folders.Columns["PID"]) { Nested = true }; ds.Relations.Add(dr); ultraTree1.DataSource = folders; ultraTree1.ExpandAll(); // ****************************************************** // TEST NODE HIGHLIGHTING // ****************************************************** UltraTreeNode node = ultraTree1.Nodes[5]; // folder E ultraTree1.ActiveNode = node; node.Selected = true; ultraTree1.Focus(); // ****************************************************** // ****************************************************** } private void DeleteFolder(UltraTreeNode node) { // delete all child folders and files var descendantFolderIds = GetAllFolderDescendants(node); descendantFolderIds.Reverse(); string parentId = node.Cells["ID"].Text; foreach (var folderId in descendantFolderIds) { // delete folder files DeleteFolderFiles(folderId); // delete folder ds.Tables["Folders"].Rows.Cast<DataRow>().First(x => x.ItemArray[0].ToString() == folderId).Delete(); } // delete parent folder and files DeleteFolderFiles(parentId); ds.Tables["Folders"].Rows.Cast<DataRow>().First(x => x.ItemArray[0].ToString() == parentId).Delete(); ds.AcceptChanges(); ultraTree1.SelectedNodes.Clear(); ultraTree1.Refresh(); } private void DeleteFolderFiles(string folderKey) { var rows = ds.Tables["Files"].Rows.Cast<DataRow>().Where(x => x.ItemArray[2].ToString() == folderKey); foreach (DataRow row in rows) { row.Delete(); } ds.AcceptChanges(); } private void DeleteFiles() { if (ultraListView1.SelectedItems.Count > 0) { string folderId = ultraTree1.ActiveNode.Cells["ID"].Text; var keys = ultraListView1.SelectedItems.Select(x => x.Key).ToArray(); for (int i = 0; i < keys.Count(); i++) { var row = ds.Tables["Files"].Rows.Cast<DataRow>().First(x => x.ItemArray[0].ToString() == keys[i]); row.Delete(); var item = ultraListView1.Items[keys[i]]; ultraListView1.Items.Remove(item); } ds.AcceptChanges(); ultraTree1.ActiveNode.BringIntoView(); ultraTree1.ActiveNode = ultraTree1.Nodes[folderId]; ultraTree1.Nodes[folderId].Selected = true; ultraTree1.HideSelection = false; ultraTree1.Select(); ultraTree1.Focus(); } } private void MoveFile(string folderId) { if (ultraListView1.SelectedItems.Count > 0) { var keys = ultraListView1.SelectedItems.Select(x => x.Key).ToArray(); for (int i = 0; i < keys.Count(); i++) { var row = ds.Tables["Files"].Rows.Cast<DataRow>().First(x => x.ItemArray[0].ToString() == keys[i]); row[2] = int.Parse(folderId); } ultraTree1.SelectedNodes.Clear(); ultraTree1.ActiveNode.BringIntoView(); ultraTree1.ActiveNode = ultraTree1.Nodes[folderId]; ultraTree1.ActiveNode.Selected = true; ultraTree1.HideSelection = false; ultraTree1.Select(); ultraTree1.Focus(); } } private void GenerateFileMoveMenu() { var parentFolders = ds.Tables["Folders"].Rows.Cast<DataRow>() .Where(x => x.ItemArray[2] == null || x.ItemArray[2].ToString() == "0").ToList(); foreach (var parent in parentFolders) { var childFolders = ds.Tables["Folders"].Rows.Cast<DataRow>() .Where(x => x.ItemArray[2].ToString() == parent.ItemArray[0].ToString()).ToList(); var folder = new ToolStripMenuItem() { Name = parent[0].ToString(), Text = parent[1].ToString() }; folder.Click += new EventHandler(this.moveMenuItem_Click); if (childFolders.Count > 0) { AddChildMoveMenuItem(parent, folder); } menuMove.DropDownItems.Add(folder); } this.contextMenuStrip1.Items.Add(menuMove); } private void AddChildMoveMenuItem(DataRow parent, ToolStripMenuItem parentFolder) { var childFolders = ds.Tables["Folders"].Rows.Cast<DataRow>() .Where(x => x.ItemArray[2].ToString() == parent.ItemArray[0].ToString()).ToList(); foreach (var folder in childFolders) { var subFolders = ds.Tables["Folders"].Rows.Cast<DataRow>() .Where(x => x.ItemArray[2].ToString() == folder.ItemArray[0].ToString()).ToList(); var subFolder = new ToolStripMenuItem() { Name = folder[0].ToString(), Text = folder[1].ToString() }; subFolder.Click += new EventHandler(this.moveMenuItem_Click); if (subFolders.Count > 0) { AddChildMoveMenuItem(folder, subFolder); } parentFolder.DropDownItems.Add(subFolder); } this.contextMenuStrip1.Items.Add(menuMove); } private List<string> GetAllFolderDescendants(UltraTreeNode node) { List<UltraTreeNode> nodes = new List<UltraTreeNode>(); this.GetAllFolderDescendants(node.Nodes, nodes); return nodes.Select(x => x.Cells["ID"].Text).ToList(); } private void GetAllFolderDescendants(TreeNodesCollection nodes, List<UltraTreeNode> descendants) { foreach (UltraTreeNode node in nodes) { descendants.Add(node); if (node.HasNodes) { this.GetAllFolderDescendants(node.Nodes, descendants); } } } private void ultraTree1_InitializeDataNode(object sender, Infragistics.Win.UltraWinTree.InitializeDataNodeEventArgs e) { if (e.Node.IsRootLevelNode && !(e.Node.Cells["PID"].Value is System.DBNull)) { e.Node.Visible = false; } else { e.Node.Visible = true; } if (ultraTree1.GetNodeByKey(e.Node.Cells["ID"].Text) == null) { e.Node.Key = e.Node.Cells["ID"].Text; } } private void ultraTree1_ColumnSetGenerated(object sender, Infragistics.Win.UltraWinTree.ColumnSetGeneratedEventArgs e) { e.ColumnSet.NodeTextColumn = e.ColumnSet.Columns["Name"]; } private void ultraTree1_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e) { this.ultraListView1.Items.Clear(); DataView dv = new DataView(ds.Tables["Files"]) { RowFilter = $"FolderID = {this.ultraTree1.ActiveNode.Cells["ID"].Value}" }; foreach (DataRow item in dv.ToTable().Rows) { this.ultraListView1.Items.Add(item.ItemArray[0].ToString(), item.ItemArray[1].ToString()); } } private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { if(e.ClickedItem.Text == "Delete") { DeleteFiles(); } } private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e) { switch (e.Tool.Key) { case "Delete": if (ultraTree1.ContainsFocus) { // delete tree folder(s) TreeEventManager eventManager = ultraTree1.EventManager; eventManager.SetEnabled(TreeEventIds.InitializeDataNode, false); DeleteFolder(ultraTree1.ActiveNode); eventManager.SetEnabled(TreeEventIds.InitializeDataNode, true); this.ultraListView1.Items.Clear(); ultraTree1.ActiveNode = ultraTree1.Nodes[0]; } else if (ultraListView1.ContainsFocus) { // delete selected files DeleteFiles(); } break; case "NewFolder": if (ultraTree1.ContainsFocus) { string value = ""; if (InputBox(ref value) == DialogResult.OK) { var parentId = ultraTree1.ActiveNode != null ? int.Parse(ultraTree1.ActiveNode.Cells["ID"].Text) : int.MinValue; var row = ds.Tables["Folders"].NewRow(); row[1] = value; row[2] = parentId; ultraTree1.DataSource = null; ds.Tables["Folders"].Rows.Add(row); ultraTree1.DataSource = ds.Tables["Folders"]; ultraTree1.ExpandAll(); } } break; case "Export": FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog { Description = "Select the directory that you want to export the selected files to.", ShowNewFolderButton = false, SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) }; DialogResult result = folderBrowserDialog.ShowDialog(); if (result == DialogResult.OK) { string folderName = folderBrowserDialog.SelectedPath; for (int i = 1; i < 3; i++) { File.WriteAllText(Path.Combine(folderName, $"test{i}.txt"), $"this is test {i}"); } } break; default: break; } } private DialogResult InputBox(ref string value) { Form form = new Form(); Label lbl = new Label() { Text = "Folder Name:" }; TextBox txt = new TextBox(); Button btnOk = new Button() { Text = "OK" }; Button btnCancel = new Button() { Text = "Cancel" }; btnOk.DialogResult = DialogResult.OK; btnCancel.DialogResult = DialogResult.Cancel; lbl.SetBounds(36, 36, 372, 13); lbl.AutoSize = true; txt.SetBounds(36, 86, 700, 20); btnOk.SetBounds(228, 160, 160, 60); btnCancel.SetBounds(400, 160, 160, 60); form.ClientSize = new Size(796, 307); form.FormBorderStyle = FormBorderStyle.FixedDialog; form.StartPosition = FormStartPosition.CenterScreen; form.MinimizeBox = false; form.MaximizeBox = false; form.Controls.AddRange(new Control[] { lbl, txt, btnOk, btnCancel }); form.AcceptButton = btnOk; form.CancelButton = btnCancel; DialogResult dialogResult = form.ShowDialog(); value = txt.Text; return dialogResult; } private void moveMenuItem_Click(object sender, EventArgs e) { MoveFile(((MenuItem)sender).Name); } private void ultraTree1_AfterLabelEdit(object sender, NodeEventArgs e) { if (string.IsNullOrWhiteSpace(e.TreeNode.Text)) { e.TreeNode.SetCellValue(e.TreeNode.Cells["Name"].Column, oldNodeText); } } private void ultraTree1_BeforeLabelEdit(object sender, CancelableNodeEventArgs e) { oldNodeText = e.TreeNode.Text; } } }
namespace UltraTree_DataBinding_CS { partial class DataTreeTest { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance("Collapsed", 11750610); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataTreeTest)); Infragistics.Win.Appearance appearance2 = new Infragistics.Win.Appearance("Expanded", 11827047); Infragistics.Win.UltraWinTree.UltraTreeNode ultraTreeNode1 = new Infragistics.Win.UltraWinTree.UltraTreeNode(); Infragistics.Win.UltraWinTree.Override _override1 = new Infragistics.Win.UltraWinTree.Override(); Infragistics.Win.Appearance appearance3 = new Infragistics.Win.Appearance(); Infragistics.Win.Appearance appearance4 = new Infragistics.Win.Appearance(); Infragistics.Win.Appearance appearance5 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.UltraToolbar ultraToolbar1 = new Infragistics.Win.UltraWinToolbars.UltraToolbar("UltraToolbar1"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool8 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Save"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool5 = new Infragistics.Win.UltraWinToolbars.ButtonTool("OpenFile"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool9 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Add"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool1 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Delete"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool2 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Export"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool12 = new Infragistics.Win.UltraWinToolbars.ButtonTool("NewFolder"); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool3 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Delete"); Infragistics.Win.Appearance appearance6 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool4 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Move"); Infragistics.Win.Appearance appearance7 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool6 = new Infragistics.Win.UltraWinToolbars.ButtonTool("OpenFile"); Infragistics.Win.Appearance appearance8 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool7 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Export"); Infragistics.Win.Appearance appearance9 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool10 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Save"); Infragistics.Win.Appearance appearance10 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool11 = new Infragistics.Win.UltraWinToolbars.ButtonTool("Add"); Infragistics.Win.Appearance appearance11 = new Infragistics.Win.Appearance(); Infragistics.Win.UltraWinToolbars.ButtonTool buttonTool13 = new Infragistics.Win.UltraWinToolbars.ButtonTool("NewFolder"); Infragistics.Win.Appearance appearance12 = new Infragistics.Win.Appearance(); this.ultraTree1 = new Infragistics.Win.UltraWinTree.UltraTree(); this.ultraListView1 = new Infragistics.Win.UltraWinListView.UltraListView(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.menuDelete = new System.Windows.Forms.ToolStripMenuItem(); this.DataTreeTest_Fill_Panel = new Infragistics.Win.Misc.UltraPanel(); this._DataTreeTest_Toolbars_Dock_Area_Left = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea(); this.ultraToolbarsManager1 = new Infragistics.Win.UltraWinToolbars.UltraToolbarsManager(this.components); this._DataTreeTest_Toolbars_Dock_Area_Right = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea(); this._DataTreeTest_Toolbars_Dock_Area_Top = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea(); this._DataTreeTest_Toolbars_Dock_Area_Bottom = new Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea(); ((System.ComponentModel.ISupportInitialize)(this.ultraTree1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.ultraListView1)).BeginInit(); this.contextMenuStrip1.SuspendLayout(); this.DataTreeTest_Fill_Panel.ClientArea.SuspendLayout(); this.DataTreeTest_Fill_Panel.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.ultraToolbarsManager1)).BeginInit(); this.SuspendLayout(); // // ultraTree1 // this.ultraTree1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); appearance1.Image = ((object)(resources.GetObject("appearance1.Image"))); appearance2.Image = ((object)(resources.GetObject("appearance2.Image"))); this.ultraTree1.Appearances.Add(appearance1); this.ultraTree1.Appearances.Add(appearance2); this.ultraTree1.DisplayStyle = Infragistics.Win.UltraWinTree.UltraTreeDisplayStyle.Standard; this.ultraTree1.ImageTransparentColor = System.Drawing.Color.Transparent; this.ultraTree1.Location = new System.Drawing.Point(13, 12); this.ultraTree1.Name = "ultraTree1"; this.ultraTree1.NodeConnectorColor = System.Drawing.SystemColors.ControlDark; ultraTreeNode1.Text = "Attachments"; this.ultraTree1.Nodes.AddRange(new Infragistics.Win.UltraWinTree.UltraTreeNode[] { ultraTreeNode1}); _override1.AllowDelete = Infragistics.Win.DefaultableBoolean.True; appearance3.Image = ((object)(resources.GetObject("appearance3.Image"))); _override1.ExpandedNodeAppearance = appearance3; _override1.LabelEdit = Infragistics.Win.DefaultableBoolean.True; appearance4.Image = ((object)(resources.GetObject("appearance4.Image"))); _override1.NodeAppearance = appearance4; appearance5.ForeColor = System.Drawing.Color.Black; _override1.SelectedNodeAppearance = appearance5; _override1.ShowEditorButtons = Infragistics.Win.UltraWinTree.ShowEditorButtons.LabelEditOnly; _override1.UseEditor = Infragistics.Win.DefaultableBoolean.True; this.ultraTree1.Override = _override1; this.ultraTree1.Size = new System.Drawing.Size(231, 437); this.ultraTree1.TabIndex = 0; this.ultraTree1.ViewStyle = Infragistics.Win.UltraWinTree.ViewStyle.Standard; this.ultraTree1.AfterLabelEdit += new Infragistics.Win.UltraWinTree.AfterNodeChangedEventHandler(this.ultraTree1_AfterLabelEdit); this.ultraTree1.AfterSelect += new Infragistics.Win.UltraWinTree.AfterNodeSelectEventHandler(this.ultraTree1_AfterSelect); this.ultraTree1.BeforeLabelEdit += new Infragistics.Win.UltraWinTree.BeforeNodeChangedEventHandler(this.ultraTree1_BeforeLabelEdit); this.ultraTree1.InitializeDataNode += new Infragistics.Win.UltraWinTree.InitializeDataNodeEventHandler(this.ultraTree1_InitializeDataNode); this.ultraTree1.ColumnSetGenerated += new Infragistics.Win.UltraWinTree.ColumnSetGeneratedEventHandler(this.ultraTree1_ColumnSetGenerated); // // ultraListView1 // this.ultraListView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.ultraListView1.ContextMenuStrip = this.contextMenuStrip1; this.ultraListView1.ItemSettings.DefaultImage = ((System.Drawing.Image)(resources.GetObject("ultraListView1.ItemSettings.DefaultImage"))); this.ultraListView1.Location = new System.Drawing.Point(251, 12); this.ultraListView1.Name = "ultraListView1"; this.ultraListView1.Size = new System.Drawing.Size(399, 437); this.ultraListView1.TabIndex = 1; this.ultraListView1.Text = "ultraListView1"; this.ultraListView1.View = Infragistics.Win.UltraWinListView.UltraListViewStyle.List; // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.menuDelete}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(108, 26); this.contextMenuStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.contextMenuStrip1_ItemClicked); // // menuDelete // this.menuDelete.Image = ((System.Drawing.Image)(resources.GetObject("menuDelete.Image"))); this.menuDelete.Name = "menuDelete"; this.menuDelete.Size = new System.Drawing.Size(107, 22); this.menuDelete.Text = "Delete"; // // DataTreeTest_Fill_Panel // // // DataTreeTest_Fill_Panel.ClientArea // this.DataTreeTest_Fill_Panel.ClientArea.Controls.Add(this.ultraListView1); this.DataTreeTest_Fill_Panel.ClientArea.Controls.Add(this.ultraTree1); this.DataTreeTest_Fill_Panel.Cursor = System.Windows.Forms.Cursors.Default; this.DataTreeTest_Fill_Panel.Dock = System.Windows.Forms.DockStyle.Fill; this.DataTreeTest_Fill_Panel.Location = new System.Drawing.Point(0, 27); this.DataTreeTest_Fill_Panel.Name = "DataTreeTest_Fill_Panel"; this.DataTreeTest_Fill_Panel.Size = new System.Drawing.Size(662, 461); this.DataTreeTest_Fill_Panel.TabIndex = 0; // // _DataTreeTest_Toolbars_Dock_Area_Left // this._DataTreeTest_Toolbars_Dock_Area_Left.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping; this._DataTreeTest_Toolbars_Dock_Area_Left.BackColor = System.Drawing.SystemColors.Control; this._DataTreeTest_Toolbars_Dock_Area_Left.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Left; this._DataTreeTest_Toolbars_Dock_Area_Left.ForeColor = System.Drawing.SystemColors.ControlText; this._DataTreeTest_Toolbars_Dock_Area_Left.Location = new System.Drawing.Point(0, 27); this._DataTreeTest_Toolbars_Dock_Area_Left.Name = "_DataTreeTest_Toolbars_Dock_Area_Left"; this._DataTreeTest_Toolbars_Dock_Area_Left.Size = new System.Drawing.Size(0, 461); this._DataTreeTest_Toolbars_Dock_Area_Left.ToolbarsManager = this.ultraToolbarsManager1; // // ultraToolbarsManager1 // this.ultraToolbarsManager1.DesignerFlags = 1; this.ultraToolbarsManager1.DockWithinContainer = this; this.ultraToolbarsManager1.DockWithinContainerBaseType = typeof(System.Windows.Forms.Form); ultraToolbar1.DockedColumn = 0; ultraToolbar1.DockedRow = 0; ultraToolbar1.NonInheritedTools.AddRange(new Infragistics.Win.UltraWinToolbars.ToolBase[] { buttonTool8, buttonTool5, buttonTool9, buttonTool1, buttonTool2, buttonTool12}); ultraToolbar1.Text = "UltraToolbar1"; this.ultraToolbarsManager1.Toolbars.AddRange(new Infragistics.Win.UltraWinToolbars.UltraToolbar[] { ultraToolbar1}); appearance6.Image = ((object)(resources.GetObject("appearance6.Image"))); buttonTool3.SharedPropsInternal.AppearancesSmall.Appearance = appearance6; buttonTool3.SharedPropsInternal.Caption = "Delete"; appearance7.Image = ((object)(resources.GetObject("appearance7.Image"))); buttonTool4.SharedPropsInternal.AppearancesSmall.Appearance = appearance7; buttonTool4.SharedPropsInternal.Caption = "Move"; appearance8.Image = ((object)(resources.GetObject("appearance8.Image"))); buttonTool6.SharedPropsInternal.AppearancesSmall.Appearance = appearance8; buttonTool6.SharedPropsInternal.Caption = "OpenFile"; appearance9.Image = ((object)(resources.GetObject("appearance9.Image"))); buttonTool7.SharedPropsInternal.AppearancesSmall.Appearance = appearance9; buttonTool7.SharedPropsInternal.Caption = "Export"; appearance10.Image = ((object)(resources.GetObject("appearance10.Image"))); buttonTool10.SharedPropsInternal.AppearancesSmall.Appearance = appearance10; buttonTool10.SharedPropsInternal.Caption = "Save"; appearance11.Image = ((object)(resources.GetObject("appearance11.Image"))); buttonTool11.SharedPropsInternal.AppearancesSmall.Appearance = appearance11; buttonTool11.SharedPropsInternal.Caption = "Add"; appearance12.Image = ((object)(resources.GetObject("appearance12.Image"))); buttonTool13.SharedPropsInternal.AppearancesSmall.Appearance = appearance12; buttonTool13.SharedPropsInternal.Caption = "NewFolder"; this.ultraToolbarsManager1.Tools.AddRange(new Infragistics.Win.UltraWinToolbars.ToolBase[] { buttonTool3, buttonTool4, buttonTool6, buttonTool7, buttonTool10, buttonTool11, buttonTool13}); this.ultraToolbarsManager1.ToolClick += new Infragistics.Win.UltraWinToolbars.ToolClickEventHandler(this.ultraToolbarsManager1_ToolClick); // // _DataTreeTest_Toolbars_Dock_Area_Right // this._DataTreeTest_Toolbars_Dock_Area_Right.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping; this._DataTreeTest_Toolbars_Dock_Area_Right.BackColor = System.Drawing.SystemColors.Control; this._DataTreeTest_Toolbars_Dock_Area_Right.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Right; this._DataTreeTest_Toolbars_Dock_Area_Right.ForeColor = System.Drawing.SystemColors.ControlText; this._DataTreeTest_Toolbars_Dock_Area_Right.Location = new System.Drawing.Point(662, 27); this._DataTreeTest_Toolbars_Dock_Area_Right.Name = "_DataTreeTest_Toolbars_Dock_Area_Right"; this._DataTreeTest_Toolbars_Dock_Area_Right.Size = new System.Drawing.Size(0, 461); this._DataTreeTest_Toolbars_Dock_Area_Right.ToolbarsManager = this.ultraToolbarsManager1; // // _DataTreeTest_Toolbars_Dock_Area_Top // this._DataTreeTest_Toolbars_Dock_Area_Top.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping; this._DataTreeTest_Toolbars_Dock_Area_Top.BackColor = System.Drawing.SystemColors.Control; this._DataTreeTest_Toolbars_Dock_Area_Top.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Top; this._DataTreeTest_Toolbars_Dock_Area_Top.ForeColor = System.Drawing.SystemColors.ControlText; this._DataTreeTest_Toolbars_Dock_Area_Top.Location = new System.Drawing.Point(0, 0); this._DataTreeTest_Toolbars_Dock_Area_Top.Name = "_DataTreeTest_Toolbars_Dock_Area_Top"; this._DataTreeTest_Toolbars_Dock_Area_Top.Size = new System.Drawing.Size(662, 27); this._DataTreeTest_Toolbars_Dock_Area_Top.ToolbarsManager = this.ultraToolbarsManager1; // // _DataTreeTest_Toolbars_Dock_Area_Bottom // this._DataTreeTest_Toolbars_Dock_Area_Bottom.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping; this._DataTreeTest_Toolbars_Dock_Area_Bottom.BackColor = System.Drawing.SystemColors.Control; this._DataTreeTest_Toolbars_Dock_Area_Bottom.DockedPosition = Infragistics.Win.UltraWinToolbars.DockedPosition.Bottom; this._DataTreeTest_Toolbars_Dock_Area_Bottom.ForeColor = System.Drawing.SystemColors.ControlText; this._DataTreeTest_Toolbars_Dock_Area_Bottom.Location = new System.Drawing.Point(0, 488); this._DataTreeTest_Toolbars_Dock_Area_Bottom.Name = "_DataTreeTest_Toolbars_Dock_Area_Bottom"; this._DataTreeTest_Toolbars_Dock_Area_Bottom.Size = new System.Drawing.Size(662, 0); this._DataTreeTest_Toolbars_Dock_Area_Bottom.ToolbarsManager = this.ultraToolbarsManager1; // // DataTreeTest // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(662, 488); this.Controls.Add(this.DataTreeTest_Fill_Panel); this.Controls.Add(this._DataTreeTest_Toolbars_Dock_Area_Left); this.Controls.Add(this._DataTreeTest_Toolbars_Dock_Area_Right); this.Controls.Add(this._DataTreeTest_Toolbars_Dock_Area_Bottom); this.Controls.Add(this._DataTreeTest_Toolbars_Dock_Area_Top); this.Name = "DataTreeTest"; this.Text = "DataTreeTest"; ((System.ComponentModel.ISupportInitialize)(this.ultraTree1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.ultraListView1)).EndInit(); this.contextMenuStrip1.ResumeLayout(false); this.DataTreeTest_Fill_Panel.ClientArea.ResumeLayout(false); this.DataTreeTest_Fill_Panel.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.ultraToolbarsManager1)).EndInit(); this.ResumeLayout(false); } #endregion private Infragistics.Win.UltraWinTree.UltraTree ultraTree1; private Infragistics.Win.UltraWinListView.UltraListView ultraListView1; private Infragistics.Win.Misc.UltraPanel DataTreeTest_Fill_Panel; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem menuDelete; private Infragistics.Win.UltraWinToolbars.UltraToolbarsManager ultraToolbarsManager1; private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _DataTreeTest_Toolbars_Dock_Area_Left; private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _DataTreeTest_Toolbars_Dock_Area_Right; private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _DataTreeTest_Toolbars_Dock_Area_Bottom; private Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea _DataTreeTest_Toolbars_Dock_Area_Top; } }
See line 98
I have placed the code you have provided into my sample, and I was able to reproduce the behavior you are seeing. I believe the issue you are seeing here is due to 2 things:
1. I believe that trying to set the Active and Selected node right after you assign the data source is likely too early and you are hitting a timing issue between trying to set these things and when the tree has actually finished loading. I would recommend setting this in the Form’s Load event instead.
2. After checking the node that is returned by ultraTree.Nodes[5], I found that the Visible property of that node was false. This is probably due to the code that you have in the InitializeDataNode event for the UltraTree that is modifying the node visibility. I have changed the initial activation check to use the following, and everything is working as expected:
UltraTreeNode firstVisibleNode = null;
for (int i = 0; i < ultraTree1.Nodes.Count; i++) { UltraTreeNode node = ultraTree1.Nodes[i];
if (node.Visible) { firstVisibleNode = node; break; } }
if (firstVisibleNode != null) { ultraTree1.ActiveNode = firstVisibleNode; firstVisibleNode.Selected = true; }
Where did you add this new code? In ultraTree1_InitializeDataNode?
the only reason I was using InitializeDataNode is that if I don't, many of the folders are duplicated. Notice the highlighting is working if I don't use InitializeDataNode, but it's on one of the duplicated nodes. Am I doing something wrong that is causing this, or do I always have to use InitializeDataNode to keep it from happening?
I changed it to this, which fixed the duplication, but now the highlighting is gone again. The other reason for using InitializeDataNode was to set the Key value of the node, which is not set automatically when databound.
if (e.Node.IsRootLevelNode && e.Node.Cells["PID"].Value != DBNull.Value) { e.Node.Visible = false; return; }
The GetNodeByKey method of the UltraTree is recursive, but it may not work as expected if the nodes have similar / equal keys as it expects a unique, case insensitive key to be passed.
If you have similar or equal keys for multiple nodes in the UltraTree, I would recommend doing a recursive search of the visible nodes, as you had mentioned.
Thank you very much for all your help so far. We can stop here if you like. I just wanted to share an update.
When all the nodes are displayed using the existing data relation and no code to hide nodes in InitializeDataNode, it does highlight the correct folder. However, it's highlighting the last one in the collection, as you can see from the screenshot I posted earlier (pasted below). I guess because the keys are duplicated, it's returning the last one it finds, or maybe it's only looking at the parent nodes and not searching for all nodes at all levels, which would make sense, but I thought GetNodeByKey did a recursive search?
I guess if I need an exact node, I have to build a method to do a recursive search of visible nodes.
I added the code in the Load event of the Form – as mentioned above, as if you are doing this right after assigning the data source to the tree, I believe you are hitting a timing issue where the tree has not actually finished loading yet.
Regarding the duplication, this is happening because of the DataRelation that you are defining:
DataRelation dr = new DataRelation("RR", folders.Columns["ID"], folders.Columns["PID"])
Since both the parent column and the child column of the DataRelation are in the same table, this creates a recursive, repeating layout in the UltraTree ‘n’ layers deep, where ‘n’ will be as long as the ID and PID of the current nodes match.
For example, taking the first node structure of A => E => F => G into account, this is happening because A is a parent-level tree node, but its ID matches the E node’s PID, and so E is a child of A. Following down this route, E has an ID of 5, which matches node F’s PID. Therefore, F is a child of E per your DataRelation. Finally, G is a child of F because G’s PID of 6 matches F’s ID of 6. The only reason it stops here is because there is no other entry in the “folders” DataTable with a PID that matches G’s ID of 7. If you are looking to prevent the current structure that is happening, I would recommend reviewing this table as you currently have a recursive, repeating DataRelation.