简体   繁体   中英

How to get the ID number from a selected treeView node?

I have a TreeView that is populated with data from a SQL Server database table, I want to insert data into it based on the selected item ID in the database, this is my code:

    private void Form1_Load(object sender, EventArgs e)
    {
        TreeLoad();
    }

    // Connecting to Database and retieving data
    private DataTable Select(int ID)
    {
        SqlConnection conn = new SqlConnection(@"server= M_SHAWAF\ORCHESTRATE; integrated security= true; database= FM");
        conn.Open();
        SqlCommand cmd = new SqlCommand("GetNodes2", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter parameter = new SqlParameter("ID", ID);
        parameter.SqlDbType = SqlDbType.Int;
        cmd.Parameters.Add(parameter);
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        return dt;
    }

    //Loading the treeView1
    void TreeLoad()
    {
        DataTable dt = Select(0);
        AddNodes(dt, treeView1.Nodes);
    }

    //Adding nodes to treeView1
    void AddNodes(DataTable dt, TreeNodeCollection coll)
    {
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode node = new TreeNode();
            node.Text = dr[1].ToString();
            coll.Add(node);

            try
            {
                DataTable child = Select((int)dr[0]);

                if (child.Rows.Count > 0)
                {
                    AddNodes(child, node.Nodes);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

This is the table:

在此处输入图片说明

The form:

在此处输入图片说明

I want to get the EmpID of the selected node when I click insert btn (there is another windows form that has a textbox for the new name).

GetNodes stored procedure:

create procedure GetNodes 
   @ID int
as  
   select 
      e.EmpID, e.EmpName 
   from 
      Employee e 
   where 
      isnull(e.ManagerID, 0) = @ID 

   return

One option is to store the ID in each node's Tag. Then you can grab it when the button is clicked by calling treeView1.SelectedNode.Tag .

you need to loop for each node on tree view the way as you populate it from database for example.

    void SelectNodeOnTreeView(string EmpID, TreeNodeCollection NodeCollection) 
    {
        if (NodeCollection.Count > 0)
        {
            foreach (TreeNode node in NodeCollection)
            {
                if (node.Text == EmpID)
                {
                    treeView1.SelectedNode = node;
                    break;
                }
                SelectNodeOnTreeView(EmpID, node.Nodes);

            }
        }
    }

You could create a your own TreeNode class which implements TreeNode but also add other necessary fields such as id :

class myTreeNode : TreeNode
{
    public int id;

    public myTreeNode(string text, int id)
    {
        this.Text = text;
        this.id = id;
    }
}

then in your AddNodes method in the foreach loop, instead of:

TreeNode node = new TreeNode();
node.Text = dr[1].ToString();

do something like this:

TreeNode node = new myTreeNode(dr[1].ToString(), (int)dr[0]); //don't know where you're storing your ID, here I'm assuming it's in dr[0].

This way, you should be able to get the id of the selected node by doing something like this:

myTreeNode selectedNode = (myTreeNode)treeview1.SelectedNode;
int selectedID = selectedNode.id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM