1

I have a TreeView that gets data from Sql Server Table. My code populates parent and child nodes successfully. I only want to know how to get the ID filed of the Node in a TextBox when I select any Node.

the ID column name is : cabinetID

Here is the code I use to populate the TreeView:

        public void loadContainerTree()
    {
       // fMain fm = new fMain();
       // txtRepositoryID.Text = fm.repositoryID.Text;
        repositoryid = Convert.ToInt32(txtRepositoryID.Text);

        conn.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM tblCabinets WHERE ParentID IS NULL AND repositoryID = @RepositoryID", conn);
        adapter.SelectCommand.Parameters.AddWithValue("@RepositoryID", repositoryid);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        RadTreeNode parentNode;
        foreach (DataRow dr in dt.Rows)
        {
            parentNode = ContainersTree.Nodes.Add(dr["CabinetName"].ToString());
            PopulateTreeView(dr["CabinetID"].ToString(), parentNode);
        }
        ContainersTree.ExpandAll();
        conn.Close();
    }

    private void PopulateTreeView(string parentid, RadTreeNode parentNode)
    {
        SqlDataAdapter adapterchild = new SqlDataAdapter("SELECT * FROM tblCabinets WHERE ParentID = @ParentID AND repositoryID = @RepositoryID", conn);
        adapterchild.SelectCommand.Parameters.AddWithValue("@ParentID", parentid);
        adapterchild.SelectCommand.Parameters.AddWithValue("@RepositoryID", repositoryid);
        DataTable dtchild = new DataTable();
        adapterchild.Fill(dtchild);

        foreach(DataRow dr in dtchild.Rows)
        {
            RadTreeNode childNode;
            if (parentNode == null)
            {
                childNode = ContainersTree.Nodes.Add(dr["cabinetName"].ToString());
            }
            else
            {

                childNode = parentNode.Nodes.Add(dr["cabinetName"].ToString());
                PopulateTreeView(dr["cabinetID"].ToString(), childNode);
            }
        }

    }
Emad Mohamed
  • 97
  • 2
  • 10

1 Answers1

0

Basic workflow with TreeView/Tag or any other control.

First of all Tag is an Object that contains data about the control. The default is null which means we can use tag to store string or int or any CustomClass

Now let's create Custom class like this:

public class Cabinet
{
    public int Id { get; set; }
    public string Name { get; set; }
    // Any other value
}

Now we will get data from our database and store it into tree node like this:

public static void AddNodeToTreeView(this TreeView tv, string Text, Cabinet cabinet)
{
    TreeNode n = new TreeNode();
    n.Text = Text;
    n.Tag = cabinet;
    tv.Add(n);
}

We call this like this:

Cabinet c = new Cabinet();
c.Id = 1;//You can store data from sql here
c.Name = "Some Name"; //You can store data from sql here
yourTreeView.AddNodeToTreeView("SomeTextTOBeDisplayed", c);

And now all you need to do is get Node you need either from event:

private void treeView1_Click(object sender, EventArgs e)
{
    Cabinet c = yourTreeView.SelectedNode.Tag as Cabinet;

    // Here do whatever you want with data
}

or from loop:

foreach(TreeNode n in yourTreeView.Nodes)
{
    Cabinet c = n.Tag as Cabinet;
}
Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54
  • I appreciate you kind reply, but could you please explain your answer in relevance to my code above. – Emad Mohamed Oct 12 '18 at 12:11
  • @EmadMohamed apply it to your code. Problem in your code is you never assign object into `Tag` property but then you use `Tag` and it returns `System.NullReferenceException` – Aleksa Ristic Oct 13 '18 at 14:09