Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows Forms VB.NET - Populate TreeView with Hierarchical data
    primarykey
    data
    text
    <p>Aloha, I'm trying to populate a treeview on a windows form app with Hierarchical data from a SQL db.</p> <p>The structure from the database is:</p> <pre><code>id_def id_parent description 1 NULL Multidificiência 2 NULL Síndrome 3 NULL Outros 4 1 Surdez de Transmissão 5 2 Surdez Neurossensorial Ligeira 6 3 Surdez Neurossensorial Média </code></pre> <p>the records with NULL value at id_parent, are the main categories, and the ones with their id_parent, are sub categories.</p> <p>Can anyone help with the code to populate the TreeView ? I managed to do it with an ASP.NET app, if it helps, here's the code:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) PopulateRootLevel(); } private void PopulateRootLevel() { SqlConnection objConn = new SqlConnection("Data Source=1.1.1.1;Initial Catalog=DREER_EDUCANDOS2006;User ID=sre_web;Password=xxx"); SqlCommand objCommand = new SqlCommand("select id_deficiencia,descricao,(select count(*) FROM NecessidadesEspeciais WHERE id_deficiencia_pai=sc.id_deficiencia) childnodecount FROM NecessidadesEspeciais sc where id_deficiencia_pai IS NULL", objConn); SqlDataAdapter da = new SqlDataAdapter(objCommand); DataTable dt = new DataTable(); da.Fill(dt); PopulateNodes(dt, TreeView1.Nodes); } private void PopulateSubLevel(int parentid, TreeNode parentNode) { SqlConnection objConn = new SqlConnection("Data Source=1.1.1.1;Initial Catalog=DREER_EDUCANDOS2006;User ID=sre_web;Password=xxx"); SqlCommand objCommand = new SqlCommand("select id_deficiencia,descricao,(select count(*) FROM NecessidadesEspeciais WHERE id_deficiencia_pai=sc.id_deficiencia) childnodecount FROM NecessidadesEspeciais sc where id_deficiencia_pai=@id_deficiencia_pai", objConn); objCommand.Parameters.Add("@id_deficiencia_pai", SqlDbType.Int).Value = parentid; SqlDataAdapter da = new SqlDataAdapter(objCommand); DataTable dt = new DataTable(); da.Fill(dt); PopulateNodes(dt, parentNode.ChildNodes); } protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e) { PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node); } private void PopulateNodes(DataTable dt, TreeNodeCollection nodes) { foreach (DataRow dr in dt.Rows) { TreeNode tn = new TreeNode(); tn.Text = dr["descricao"].ToString(); tn.Value = dr["id_deficiencia"].ToString(); nodes.Add(tn); //If node has child nodes, then enable on-demand populating tn.PopulateOnDemand = ((int)(dr["childnodecount"]) &gt; 0); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload