• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Select the treeview-node that has been created

HI,

I have this code in the code section to create a new node in a treeview.
And it works like a clock. Only when I make a new node, the node that
has been created is not selected. How can i do that?

Peter
private void tvSample1RightClickInsert(object sender, System.EventArgs e)
        {
            try
            {
                TreeNode node = tvNotes.SelectedNode;
                if (node == null) { return; }
                InsertNewNode(node);
            }
            catch (Exception err) 
            { 
                MessageBox.Show(err.Message); 
            }
        }

private void InsertNewNode(TreeNode node)
        {
         DataRow row = null;
         DataRow ParentRow = null;
         DataTable dt = null;
         int newindex = 0;
         try
         {
             ParentRow = (DataRow)node.Tag; 
             if (ParentRow == null) { return; }
             newindex = int.Parse(ParentRow["SortOrder"].ToString()) + 1;
             dt = ParentRow.Table;
             row = dt.NewRow();
             row["ModelID"] = int.Parse(ParentRow["ModelID"].ToString());
			 row["NodeID"] = Guid.NewGuid().ToString();
			 row["ParentNodeID"] = ParentRow[dt.PrimaryKey[0].ColumnName].ToString();
             row["Description"] = "New Node";
			 row["ImageIndex"] = 1;
			 row["SelectedImageIndex"] = 2;
             row["SortOrder"] = newindex;
             dt.Rows.Add(row);
             node.Nodes.Add(GetTreeNodeFromDataRow(row,"Description")); 
         }
         catch (Exception) 
		 {
			 throw;
		 }
      }

Open in new window

0
peterkiers
Asked:
peterkiers
  • 3
  • 2
1 Solution
 
HainKurtSr. System AnalystCommented:
maybe this:

node.Nodes.Add(GetTreeNodeFromDataRow(row,"Description"));
-->
node.Nodes.Add(newNode)
node.SelectedNode = newNode
0
 
HainKurtSr. System AnalystCommented:
try this:

node.Nodes.Add(GetTreeNodeFromDataRow(row,"Description"));

-->

TreeNode newNode = GetTreeNodeFromDataRow(row,"Description");
node.Nodes.Add(newNode);
tvNotes.SelectedNode = newNode;
0
 
peterkiersAuthor Commented:
I haven't put all the code in the code-section.
And to tell you the treu I can't read this code.
P.
private void tvSample1RightClickInsert(object sender, System.EventArgs e)
        {
            try
            {
                TreeNode node = tvNotes.SelectedNode;
                if (node == null) { return; }
                InsertNewNode(node);
            }
            catch (Exception err) 
            { 
                MessageBox.Show(err.Message); 
            }
        }

private void InsertNewNode(TreeNode node)
        {
         DataRow row = null;
         DataRow ParentRow = null;
         DataTable dt = null;
         int newindex = 0;
         try
         {
             ParentRow = (DataRow)node.Tag; 
             if (ParentRow == null) { return; }
             newindex = int.Parse(ParentRow["SortOrder"].ToString()) + 1;
             dt = ParentRow.Table;
             row = dt.NewRow();
             row["ModelID"] = int.Parse(ParentRow["ModelID"].ToString());
			 row["NodeID"] = Guid.NewGuid().ToString();
			 row["ParentNodeID"] = ParentRow[dt.PrimaryKey[0].ColumnName].ToString();
             row["Description"] = "New Node";
			 row["ImageIndex"] = 1;
			 row["SelectedImageIndex"] = 2;
             row["SortOrder"] = newindex;
             dt.Rows.Add(row);
             node.Nodes.Add(GetTreeNodeFromDataRow(row,"Description"));
         }
         catch (Exception) 
		 {
			 throw;
		 }

      }

 public static void AddNodeFromDataRow(TreeNode parentNode, DataRow row, string textColumnName, string dataRelationName)
        {
            try
            {
                foreach (DataRow childrow in row.GetChildRows(dataRelationName))
                {
                    parentNode.Nodes.Add(GetTreeNodeFromDataRow(childrow, textColumnName));
                    AddNodeFromDataRow(parentNode.LastNode, childrow, textColumnName, dataRelationName);
                }
            }
            catch (Exception) { throw; }
        }
        //*----------------------------------------------------------*//
        public static TreeNode GetTreeNodeFromDataRow(DataRow row, string textColumnName)
        {
            TreeNode child = null;
            string imageidx = "";
            string selimageidx = "";
            try
            {
                if (row.Table.Columns.Contains(ImageIndexColumnName))
                {
                    imageidx = row[ImageIndexColumnName].ToString();
                }
                if (row.Table.Columns.Contains(SelectedImageIndexColumnName))
                {
                    selimageidx = row[SelectedImageIndexColumnName].ToString();
                }
                child = new TreeNode();
                child.Text = row[textColumnName].ToString().Trim();
                if (imageidx.Length > 0)
                {
                    child.ImageIndex = Convert.ToInt32(imageidx);
                }
                if (selimageidx.Length > 0)
                {
                    child.SelectedImageIndex = Convert.ToInt32(selimageidx);
                }
                child.Tag = row;
            }
            catch (Exception) { throw; }
            return child;
        }
        //*----------------------------------------------------------*//

Open in new window

0
 
HainKurtSr. System AnalystCommented:
just replace line 36 with these lines @ 35111525

TreeNode newNode = GetTreeNodeFromDataRow(row,"Description");
node.Nodes.Add(newNode);
tvNotes.SelectedNode = newNode;
0
 
peterkiersAuthor Commented:
Yes, your right, it works thank you very much.

Peter
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

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