Solved

Select the treeview-node that has been created

Posted on 2011-03-11
5
348 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 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 35111418
maybe this:

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

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 55

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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