Click here to Skip to main content
Rate this: bad
good
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 19:39pm
Edited 16-Nov-12 23:22pm
Nelek42.1K
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 at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 685
1 Manas Bhardwaj 380
2 OriginalGriff 344
3 Abhinav S 293
4 Sampath Lokuge 245
0 Sergey Alexandrovich Kryukov 7,967
1 OriginalGriff 4,332
2 Peter Leow 3,699
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid