Solved

Select the treeview-node that has been created

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

18 Experts available now in Live!

Get 1:1 Help Now