Solved

Select the treeview-node that has been created

Posted on 2011-03-11
5
343 Views
Last Modified: 2013-12-17
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
Comment
Question by:peterkiers
  • 3
  • 2
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 35111418
maybe this:

node.Nodes.Add(GetTreeNodeFromDataRow(row,"Description"));
-->
node.Nodes.Add(newNode)
node.SelectedNode = newNode
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35111463
try this:

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

-->

TreeNode newNode = GetTreeNodeFromDataRow(row,"Description");
node.Nodes.Add(newNode);
tvNotes.SelectedNode = newNode;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 35111525
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
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 35111728
just replace line 36 with these lines @ 35111525

TreeNode newNode = GetTreeNodeFromDataRow(row,"Description");
node.Nodes.Add(newNode);
tvNotes.SelectedNode = newNode;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 35111787
Yes, your right, it works thank you very much.

Peter
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now