Tuesday, 23 April 2013

how to dynamic node populate in treeview with checkbox in c#.net



Procedure
ALTER PROCEDURE dbo.p
AS
    begin
    select m.eid,m.name,m.mgr,(select count(*) from emp where emp.mgr=m.eid) ccount from emp m left join emp t on m.mgr=t.eid
    end
    page looks like:



c# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication33
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataClasses1DataContext o = new DataClasses1DataContext();
        private void Form1_Load(object sender, EventArgs e)
        {
            fillcombox();
            filltv();
        }
        void fillcombox()
        {
            var m = (from x in o.EMP4s select new { x.EID, x.NAME }).ToList();
            comboBox1.DataSource = m;
            comboBox1.DisplayMember = "NAME";
            comboBox1.ValueMember = "EID";
        }
        void filltv()
        {
            treeView1.Nodes.Clear();
            var m = (from x in o.EMP4s where x.MGR==0 select new { x.EID, x.NAME }).ToList();
            for (int i = 0; i < m.Count; i++)
            {
                TreeNode tn = new TreeNode();
               
                tn = treeView1.Nodes.Add(m[i].EID.ToString(), m[i].NAME,m[i].EID);
                filltv1(m[i].EID, tn);
            }
        }
        void filltv1(int id, TreeNode tn1)
        {
            var m = (from x in o.EMP4s where x.MGR == id select new { x.EID, x.NAME }).ToList();
            for(int j=0;j<m.Count;j++)
            {
                TreeNode tn2 = new TreeNode();
                if (tn1 == null)
                    tn2 = treeView1.Nodes.Add(m[j].EID.ToString(), m[j].NAME,m[j].EID);
                else
                    tn2 = tn1.Nodes.Add(m[j].EID.ToString(), m[j].NAME);
                filltv1(m[j].EID, tn2);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            EMP4 p = new EMP4();
            p.EID = Convert.ToInt32(textBox1.Text);
            p.NAME = textBox2.Text;
            p.MGR = Convert.ToInt32(comboBox1.SelectedValue.ToString());
            o.EMP4s.InsertOnSubmit(p);
            o.SubmitChanges();
            fillcombox();
            filltv();
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked)
                foreach (TreeNode tn in e.Node.Nodes)
                    tn.Checked = true;
            else
                foreach (TreeNode tn in e.Node.Nodes)
                    tn.Checked = false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (TreeNode tn in treeView1.Nodes)
                MessageBox.Show(tn.ImageIndex.ToString());
        }

       
    }
}
 

No comments:

Post a Comment