Click here to Skip to main content
15,896,154 members

how to place data in treeview from database

Umapathi K asked:

Open original thread
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




C#
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]
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900