Treeview with Nodes & Childs via SQL database

Asked

Viewed 388 times

2

I wonder how can I popular a TreeView with Nodes and Childs via an SQL connection. The connection is already done this is the code I have for now, but it does not work because it adds only a Node that is the first 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);
    }

}

1 answer

0

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

You can take advantage of the logic of filling out treeview, but recommend not using Datatable and fit 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 must 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 Register 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 });
  • Thanks for the answer, I’ll see what I can do. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.