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);
}
}
}