windows tree view data binding from data base

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jakki
    New Member
    • Dec 2007
    • 5

    windows tree view data binding from data base

    hi every one..

    i am binding data in a tree view using windows C# .. auctually i finished most of the code.. but i did't know how to proceed further .. i attached the code hear.. as well as it display the parent id.. insted of group_name ...


    private void Form1_Load(obje ct sender, EventArgs e)
    {
    PopulateRootLev el();
    }

    private void PopulateRootLev el()
    {
    SqlConnection objConn = new SqlConnection(" Data Source=CDTECHPR O2\\SQLEXPRESS; Initial Catalog=Image;I ntegrated Security=True") ;
    SqlCommand objCommand = new SqlCommand("sel ect ParentId,GroupN ame,(select count(*) FROM tree WHERE SubGroup=sc.Par entId) childnodecount FROM tree sc where SubGroup IS NULL", objConn);
    SqlDataAdapter da = new SqlDataAdapter( objCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);
    PopulateNodes(d t, treeView1.Nodes );
    }

    private void PopulateNodes(D ataTable dt, TreeNodeCollect ion nodes)
    {
    int NodeIndex = 0;
    foreach (DataRow dr in dt.Rows)
    {
    TreeNode tn = new TreeNode();
    tn.Name = dr["GroupName"].ToString();
    tn.Text = dr["ParentId"].ToString();
    nodes.Add(tn);

    if ((int)dr["childnodecount "] > 0)
    {
    NodeIndex++;
    }
    else
    {
    NodeIndex = 0;
    }
    }
    }

    private void treeView1_After Select(object sender, TreeViewEventAr gs e)
    {
    int s = int.Parse(e.Nod e.Text);
    PopulateSubLeve l(s, e.Node);
    }

    private void PopulateSubLeve l(int parentid, TreeNode parentNode)
    {
    SqlConnection objConn = new SqlConnection(" Data Source=CDTECHPR O2\\SQLEXPRESS; Initial Catalog=Image;I ntegrated Security=True") ;
    SqlCommand objCommand = new SqlCommand("sel ect ParentId,GroupN ame,(select count(*) FROM tree " + "WHERE SubGroup=sc.Par entId) childnodecount FROM tree sc where SubGroup=@SubGr oup", objConn);
    objCommand.Para meters.Add("@Su bGroup", SqlDbType.Int). Value = parentid;
    SqlDataAdapter da = new SqlDataAdapter( objCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);
    PopulateNodes(d t, parentNode.Node s);
    }

    private void treeView1_After Check(object sender, TreeViewEventAr gs e)
    {
    int s = int.Parse(e.Nod e.Text);
    PopulateSubLeve l(s, e.Node);
    }

    the datadatse is

    parentid groupname subgroup

    11 tree1 Null
    12 tree2 Null
    13 node1 11
    14 node2 12
    15 newnode 13



    any one can help me..

    regards
    jagatheesan.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    Your tn.Text = dr["ParentId"].ToString(); is pointing to Parentid,that why u r gettting parent id as displayed text

    Comment

    Working...