TreeView with Nodes & Childs through SQL database

1

I would like to know how I can TreeView popular with Nodes and Childs through an SQL connection. The connection is already done this is the code I have for now, but it does not work since it only adds a node that is the first one in the list.

n01 - ParentNode
n02 - ChildNode

foreach (DataRow dr in tb.Rows)
{
    if (treeView_menus.Nodes.Count > 0)
    {
        if (treeView_menus.TopNode.Tag == dr.ItemArray[1])
        {
            TreeNode node = new TreeNode();
            node.Tag = dr["Id02"];
            node.Text = dr["n02"].ToString();

            treeView_menus.Nodes[dr["n01"].ToString()].Nodes.Add(node);
        }

    }
    else
    {
        TreeNode node = new TreeNode();
        node.Tag = dr["Id01"];
        node.Text = dr["n01"].ToString();
        treeView_menus.Nodes.Add(node);
    }

}
    
asked by anonymous 27.06.2018 / 14:23

1 answer

0

I made this method that I used a few years ago, and even so I still used a DataTable as data entry, if I were to do it again, I would use a List of an object. It would be easier.

  

You can take advantage of the logic of populating the treeview, but I recommend not using DataTable and fitting it into a typed list.

public static void PreencherMenu(DataTable dtMenu, TreeView tree)
{
    if (tree != null)
    {    
        tree.Nodes.Clear();

        dtMenu.DefaultView.Sort = "nivel, ordem";
        dtMenu = dtMenu.DefaultView.ToTable();

        foreach (DataRow r in dtMenu.Rows)
        {
            string nome = r["nome"].ToString();
            string pai = r["pai"].ToString();

            TreeNode t = new TreeNode(r["texto"].ToString());
            t.ImageKey = r["img"].ToString();
            t.SelectedImageKey = r["simg"].ToString();
            t.Name = r["nome"].ToString();
            t.Text = r["texto"].ToString();
            t.Tag = String.IsNullOrEmpty(r["tag"].ToString()) ? null : r["tag"].ToString();

            if (String.IsNullOrEmpty(pai))
            {
                tree.Nodes.Add(t);
            }
            else
            {
                TreeNode[] parent = tree.Nodes.Find(pai, true);
                if (parent.Length != 0)
                {
                    parent[0].Nodes.Add(t);
                }
            }

        }
    }
}

The input table should have the following format:

DataTable dt = new DataTable();

dt.Columns.Add("texto"); //Texto de Exibição do item
dt.Columns.Add("nome");  //Nome do item (chave) deve ser único
dt.Columns.Add("pai");   //Nome do item pai do item atual, se não houver um pai, informar null
dt.Columns.Add("img");   //Index de uma imagem a ser usada em um imagelist associado ao controle
dt.Columns.Add("simg");  //Index de uma imagem a ser usada em um imagelist associado ao controle (quando o item for selecionado)
dt.Columns.Add("tag");   //Tag associada ao item
dt.Columns.Add("nivel", typeof(Int32));//Nivel do item (0 = Raiz)
dt.Columns.Add("ordem", typeof(Int32));//Ordem que o item deve aparecer

dt.PrimaryKey = new DataColumn[] { dt.Columns["nome"] };

Example of a Registration menu:

dt.Rows.Add(new object[] { "Cadastros", "cadastros", null, "Folder-Close.png", "Folder-Open.png", null, 0, 1 });

dt.Rows.Add(new object[] { "Pessoas / Convênios", "pessoas", "cadastros", "Clients-icon.png", "Clients-icon.png", null, 1, 1 });
    
27.06.2018 / 15:05