I am trying to Populate the treeview with the backgroundworker control but did not get the parent and child nodes in
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;
using System.Configuration;
using System.Data.SqlClient;
namespace TvBackGroundWorker
{
public partial class Form1 : Form
{
SqlConnection conn;
TreeNode parentNode = null;
TreeNode childNode;
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String connectionString;
connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
conn = new SqlConnection(connectionString);
String Sequel = "SELECT MAINMNU,MENUPARVAL,STATUS FROM MNUPARENT ORDER BY 2";
SqlDataAdapter da = new SqlDataAdapter(Sequel, conn);
conn.Open();
da.Fill(dt);
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
parentNode = (TreeNode)treeView1.Invoke(new Add(Add1), new object[] { dr["MAINMNU"].ToString()});
PopulateTreeView(Convert.ToInt32(dr["MENUPARVAL"].ToString()), parentNode);
}
}
private void PopulateTreeView(int parentId, TreeNode parentNode)
{
String Seqchildc = "SELECT MENUPARVAL,FRM_CODE,FRM_NAME,MNUSUBMENU FROM MNUSUBMENU WHERE MENUPARVAL=" + parentId + "";
SqlDataAdapter dachildmnuc = new SqlDataAdapter(Seqchildc, conn);
DataTable dtchildc = new DataTable();
dachildmnuc.Fill(dtchildc);
foreach (DataRow dr in dtchildc.Rows)
{
if (parentNode == null)
{ //childNode = treeView1.Nodes.Add(dr["FRM_NAME"].ToString());
childNode = (TreeNode)treeView1.Invoke(new Add(Add2), new object[] { dr["FRM_NAME"].ToString() });
}
else
{
//childNode = parentNode.Nodes.Add(dr["FRM_NAME"].ToString());
childNode = (TreeNode)treeView1.Invoke(new Add(Add2), new object[] { dr["FRM_NAME"].ToString() });
System.Threading.Thread.Sleep(1000);
}
PopulateTreeView(Convert.ToInt32(dr["MNUSUBMENU"].ToString()), childNode);
}
}
public delegate TreeNode Add(string txt);
public TreeNode Add1(string txt)
{
parentNode = treeView1.Nodes.Add(txt);
return parentNode;
}
public TreeNode Add2(string txt)
{
if(parentNode == null)
childNode = treeView1.Nodes.Add(txt);
else
childNode = parentNode.Nodes.Add(txt);
return childNode;
}
}
}
For Root or Parent Nodes
CREATE TABLE [dbo].[MNUPARENT](
[MAINMNU] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[STATUS] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MENUPARVAL] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_MNUPARENT] PRIMARY KEY CLUSTERED
(
[MENUPARVAL] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Child Nodes
CREATE TABLE [dbo].[MNUSUBMENU](
[MENUPARVAL] [int] NOT NULL,
[FRM_CODE] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[FRM_NAME] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[MNUSUBMENU] [int] NOT NULL,
[STATUS] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED
(
[MNUSUBMENU] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO
MNUSUBMENU(MENUPARVAL,FRM_NAME,MNUSUBMENU,STATUS) VALUES(1,'Child Finance',10,'Y')
INSERT INTO
MNUSUBMENU(MENUPARVAL,FRM_NAME,MNUSUBMENU,STATUS) VALUES(1,'Accounting',20,'Y')
INSERT INTO
MNUSUBMENU(MENUPARVAL,FRM_NAME,MNUSUBMENU,STATUS) VALUES(20,'Audit',30,'Y')
INSERT INTO
MNUSUBMENU(MENUPARVAL,FRM_NAME,MNUSUBMENU,STATUS) VALUES(30,'Acc. Standards',40,'Y')
Any help appreciated.