Click here to Skip to main content
12,622,032 members (35,134 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
i used below coding to fetch data from database to bind in treeview., i am getting fromdate details according to selected company from table., for example if i choose A then i am getting treeview like

A
 --20/10/2012
A
 --23/10/2012
A
 --27/10/2012
A
 --29/10/2012
but actual i want like

A
 --20/10/2012
 --23/10/2012
 --27/10/2012
 --29/10/2012



private void ShowMenu_Click(object sender, EventArgs e)
{
 
    try
    {
        gn.cnopen();
        cmd = new SqlCommand("FETCH_CONFERENCE_HALL_DETAILS_MENUS", gn.cn());
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TYPE", 2);
        cmd.Parameters.AddWithValue("@FROM_DATE", "");
        cmd.Parameters.AddWithValue("@TO_DATE", "");
        cmd.Parameters.AddWithValue("@COMPANYNAME",ddlcompanyname.SelectedItem.ToString());
 
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
 

        TreeNode trn;
 
        TreeNode node;
        TreeNode[] array;
 
            treeView1.Nodes.Clear();
            dt.Clear();
            da.Fill(dt);
 
            string cmn = dt.Rows[0]["company_name"].ToString();
           if (dt.Rows.Count > 0)
            {
               for (int cn = 0; cn <= dt.Rows.Count - 1; cn++)
                {
                    string getfrmdate = dt.Rows[cn]["fromdate"].ToString();
 
                    node = new TreeNode(getfrmdate);
                    getfrmdate = "";
                    array = new TreeNode[] { node };
                    trn = new TreeNode(cmn, array);
                    treeView1.Nodes.Add(trn);
 
               }
            }
 
    }
    catch (SqlException err)
    {
        MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

[edit]code block added[/edit]
Posted 16-Nov-12 20:39pm
Updated 17-Nov-12 0:22am
Nelek110.5K
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi

Can you try the below code

string cmn = dt.Rows[0]["company_name"].ToString();
                if (dt.Rows.Count > 0)
                {
                    TreeNode parentNode = new TreeNode();
                    parentNode.Text = cmn;
                    treeView1.Nodes.Add(parentNode);
 
                    for (int cn = 0; cn <= dt.Rows.Count - 1; cn++)
                    {
                        TreeNode child = new TreeNode();
                        child.Text = dt.Rows[cn]["fromdate"].ToString(); 
                        parentNode.Nodes.Add(child);
                    }
                }
  Permalink  
Comments
Dominic Abraham 18-Nov-12 1:29am
   
If the solution is helpful, please mark it as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100