Tree view in winforms using c#

Tree view in c#



ms sqlserver 2008(database)
__________________________


step 1
_________


CREATE TABLE [dbo].[myTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[title] [varchar](255) NOT NULL,
[parentID] [int] NULL,
 CONSTRAINT [PK_myTable] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

step 2
_________


INSERT INTO myTable(title, parentID) VALUES('Microsoft', NULL)
INSERT INTO myTable( title, parentID) VALUES('C#', 1)
INSERT INTO myTable( title, parentID) VALUES('VB.net', 1)
INSERT INTO myTable( title, parentID) VALUES('Open Source', NULL)
INSERT INTO myTable( title, parentID) VALUES('Python', 4)
INSERT INTO myTable( title, parentID) VALUES('Ruby', 4)
INSERT INTO myTable( title, parentID) VALUES('PHP', 4)
INSERT INTO myTable( title, parentID) VALUES('Perl', 4)
INSERT INTO myTable( title, parentID) VALUES('Java', 4)
INSERT INTO myTable( title, parentID) VALUES('LinQ', 2)
INSERT INTO myTable( title, parentID) VALUES('5.2', 7)
INSERT INTO myTable( title, parentID) VALUES('4.4', 7)


step 3
__________


ALTER TABLE [dbo].[myTable]  WITH CHECK ADD  CONSTRAINT [FK_myTable_myTable] FOREIGN KEY([parentID])
REFERENCES [dbo].[myTable] ([ID])
GO
ALTER TABLE [dbo].[myTable] CHECK CONSTRAINT [FK_myTable_myTable]


select*from myTable

-------------------------------------------------------------------------------------------------------------------



visual studio 2010 (winforms) using c#
______________________________________


on Pageload event (form1.cs)
_________________________________


SqlConnection conn = new SqlConnection("Data Source = sumitshekhawat; Initial Catalog = db_Sumit; user ID = sa; password = user@123");
// Connection with out user and password. 
// SqlConnection conn = new SqlConnection("Data Source = sumitshekhawat; Initial Catalog = db_Sumit; Integrated Security = True");
private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            TreeNodeCollection parentNode = treeView1.Nodes; 
            SqlDataAdapter dap = new SqlDataAdapter("SELECT ID, title, ISNULL(parentID, 0) AS parentID FROM myTable", conn);
            dap.Fill(dt); 
            PopulateTreeView(parentNode, 0, dt);
        }
protected void PopulateTreeView(TreeNodeCollection parentNode, int parentID, DataTable folders)
        {
            foreach (DataRow folder in folders.Rows)
            {
                if (Convert.ToInt32(folder["parentID"]) == parentID)
                {
                    String key = folder["ID"].ToString();
                    String text = folder["title"].ToString();
                    TreeNodeCollection newParentNode = parentNode.Add(key, text).Nodes;
                    PopulateTreeView(newParentNode, Convert.ToInt32(folder["ID"]), folders);
                }
            }
        }


Result :: 


Comments

Popular posts from this blog

how to Replace null value with 0 Using C#

how to fetch all HTML Table Records and modify all records accordingly using jquery and Javascript