Solved

Select the treeview-node that has been created

Posted on 2011-03-11
5
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 35111418
maybe this:

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

Expert Comment

by:Huseyin KAHRAMAN
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:
Huseyin KAHRAMAN 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 72
Amazon S3 Images with .Net 3 30
Using/Import statement in code doesn't see reference of dll just added. 3 26
Need help with a query 14 36
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

763 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