Populate TreeView from Database using C#

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.50 out of 5)
Loading ... Loading ...
SEOClerks Affiliate Store Responsive

load treeview from database sharpcoders 150x150 Populate TreeView from Database using C#TreeView Control is most important and useful control. Basically TreeView control allows you to display data in hierarchical form. In this tutorial, you will learn, how  TreeView Menu can be populated from databas. I’ll use User_Menu table to load data to TreeView.

 

 

 

User_Menu Structure

First of All you need to mapp User_Menu Database Table to C# DataTable, So that you can populate TreeView without intracting with database. We have created a method LoadDataTable that loads Data from Database Table to DataTable and returns DataTable.

Next Step is to create a method to load Data to TreeView. The logic is, you need to select Parent Records. For this purpose, set ParentId to 0, Parent records are such records  that have no parent. Now apply foreach loop on Master records and load its corresponding child one by one through recursion.

loadTreeMenu method is created to load Parent Nodes and loadTreeSubMenu method is created to load corresponding child nodes.

Parameters: 

  • TreeView placed on the form/Page
  • DataTable that is loaded from Database table

In loadTreeSubMenu recursion is used to load child nodes.

You can call method loadTreeMenu to load Menu to TreeView .

Thats It!!!

 

Validate TextBox For Digits, Alphabets, AlphaNumeric Characters
C# Coding Standards and Best Programming Practice Part-3

  • saba

    this is not show the nodes in runtime!

    • http://sharp-coders.com/ Anas Mir

      can you tell me, how you are using it? It works perfactly for me.

  • salimi

    I am asking about a sample treeview control that is connected to the database, including adding and modifying node by node and node and drag & drap send me an email.
    Slimiabd@yahoo.Com

  • DM498

    i have try it with sqlce and did not work
    do you have a outher way to do it. with c#

  • Arif

    please correct the code

    Change ParentNode.Nodes.Add(child); to ParentNode1.ChildNodes.Add(child);

    private void loadTreeSubMenu(ref TreeNode ParentNode, int ParentId, DataTable dtMenu)
    {
    DataRow[] childs = dtMenu.Select(“ParentCategoryCode=’” + ParentId + “‘”);
    foreach (DataRow dRow in childs)
    {
    TreeNode child = new TreeNode();
    child.Text = dRow["CategoryName"].ToString();
    ParentNode.ChildNodes.Add(child);
    //Recursion Call
    loadTreeSubMenu(ref child, int.Parse(dRow["CategoryCode"].ToString()), dtMenu);
    }
    }

  • Bharathiraja

    Thanks buddy! it is really use full to me ! thanks and thanks a lot!!!!!

  • Mansoor Pasha

    how to display referenceid and its sub sub ids in treeview c# i have referenceid which i will put it as input and i want to display all of its sub sub ids ? and its my code plzzzz correct it….

    private void button2_Click(object sender, EventArgs e)

    {

    l:

    try

    {

    con = new OleDbConnection(cs);

    con.Open();

    //Sales.ReferenceID = Customer.CustomerID and

    cmd = new OleDbCommand(“SELECT * from Sales where ReferenceID =’” + textBox22.Text + “‘ order by CustomerID ASC “, con);

    OleDbDataAdapter myDA = new OleDbDataAdapter(cmd);

    DataTable rdr = new DataTable();

    myDA.Fill(rdr);

    for (int i = 0; i < rdr.Rows.Count; i++)

    {

    TreeNode node = new TreeNode(rdr.Rows[i]["CustomerNameSales"].ToString());

    node.Nodes.Add(rdr.Rows[i]["ReferenceID"].ToString());

    node.Nodes.Add(rdr.Rows[i]["CustomerID"].ToString());

    node.Nodes.Add(rdr.Rows[i]["Rank"].ToString());

    node.Nodes.Add(rdr.Rows[i]["checkcol"].ToString());

    treeView1.Nodes.Add(node);

    }

    con.Close();

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    try

    {

    con = new OleDbConnection(cs);

    con.Open();

    String ct102000 = "SELECT * from Sales where ReferenceID = '" + textBox22.Text + "'";

    cmd = new OleDbCommand(ct102000, con);

    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    if ((rdr.Read() == true))

    {

    textBox21.Text = (rdr["CustomerID"].ToString());

    textBox22.Text = textBox21.Text;

    if (textBox22.Text != null)

    {

    goto l;

    }

    goto l;

    }

    con.Close();

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    }

  • Mansoor Pasha

    how to display referenceid and its sub sub ids in treeview c# i have referenceid which i will put it as input and i want to display all of its sub sub ids ? and its my code plzzzz correct it….

    private void button2_Click(object sender, EventArgs e)

    {

    l:

    try

    {

    con = new OleDbConnection(cs);

    con.Open();

    //Sales.ReferenceID = Customer.CustomerID and

    cmd = new OleDbCommand(“SELECT * from Sales where ReferenceID =’” + textBox22.Text + “‘ order by CustomerID ASC “, con);

    OleDbDataAdapter myDA = new OleDbDataAdapter(cmd);

    DataTable rdr = new DataTable();

    myDA.Fill(rdr);

    for (int i = 0; i < rdr.Rows.Count; i++)

    {

    TreeNode node = new TreeNode(rdr.Rows[i]["CustomerNameSales"].ToString());

    node.Nodes.Add(rdr.Rows[i]["ReferenceID"].ToString());

    node.Nodes.Add(rdr.Rows[i]["CustomerID"].ToString());

    node.Nodes.Add(rdr.Rows[i]["Rank"].ToString());

    node.Nodes.Add(rdr.Rows[i]["checkcol"].ToString());

    treeView1.Nodes.Add(node);

    }

    con.Close();

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    try

    {

    con = new OleDbConnection(cs);

    con.Open();

    String ct102000 = "SELECT * from Sales where ReferenceID = '" + textBox22.Text + "'";

    cmd = new OleDbCommand(ct102000, con);

    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    if ((rdr.Read() == true))

    {

    textBox21.Text = (rdr["CustomerID"].ToString());

    textBox22.Text = textBox21.Text;

    if (textBox22.Text != null)

    {

    goto l;

    }

    goto l;

    }

    con.Close();

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    }

  • Mansoor Pasha

    Actualy when i want to display all ids which are sub ids to the inputed id in a text box ..Means iWhen i input a id in an textbox It should display all its sub sub id’s in treeview

    • http://sharp-coders.com/ Anas Mir

      Reply with your Database Table structure and a rough sketch what you want in shape of image.

  • Mansoor Pasha

    plzz its urgent


You might also likeclose