K
Kam
I'm trying to populate a treeview from a SQL tables in C#. i've
written the SQL procedures (i.e. database wise is sorted). However, I
would like to see an example of how to retrieve the data from the
tables (recrusive method).
I finally worked out how to retrieve the data. it was just a matter of
understanding the database class I am using. However, one problem I
have at the moment is that I could display the parent nodes and the
first node subnodes which is not even what I want to display (i need
to modify the procedure).
the following is the method I created to display the data from the
table. any idea how I could display subnodes (i.e. code logic!)
//------------------------------------------------
private void buildTreeView()
{
// the connection string and command string
// "upDisplaySites" is
the SQL Procedure name .....
string connstring =
ConfigurationSettings.AppSettings["ConnectionString"];
DataTable dt =
TMG.Data.DataAccess.GetResultTable("upDisplaySites", connstring);
DataTable dt1 =
TMG.Data.DataAccess.GetResultTable("upDisplayGroups", connstring);
DataTable dt2 =
TMG.Data.DataAccess.GetResultTable("upDisplayUsers", connstring);
treeView1.Nodes.Clear();
try
{
TreeNode parentNode = new TreeNode();
foreach(DataRow dr in dt.Rows)
{
treeView1.Nodes.Add(dr["Name"].ToString());
//TreeNode tNode = new TreeNode();
parentNode = treeView1.Nodes[0];
}
// this needs to be changed!
foreach(DataRow dr1 in dt1.Rows)
{
TreeNode subNode = new TreeNode();
parentNode.Nodes.Add(dr1["Name"].ToString());
subNode = treeView1.Nodes[0];
}
}
catch (Exception ex){ MessageBox.Show("Err");}
}
}
//--------------------------------------------------------------------------------------
the above displayed the Sites as follows:
==============
-a
-1
-2
-3
-b
-c
-d
-f
==============
The idea is to display the sites then the groups within the sites and
then the users in each group. (i.e. 3 levels of nodes)
Any idea of I could logically work this out. (note: the table are as
follows: site, group, user and others involved. i.e. multiple table
are used)
Thank you in advance.
Kam
written the SQL procedures (i.e. database wise is sorted). However, I
would like to see an example of how to retrieve the data from the
tables (recrusive method).
I finally worked out how to retrieve the data. it was just a matter of
understanding the database class I am using. However, one problem I
have at the moment is that I could display the parent nodes and the
first node subnodes which is not even what I want to display (i need
to modify the procedure).
the following is the method I created to display the data from the
table. any idea how I could display subnodes (i.e. code logic!)
//------------------------------------------------
private void buildTreeView()
{
// the connection string and command string
// "upDisplaySites" is
the SQL Procedure name .....
string connstring =
ConfigurationSettings.AppSettings["ConnectionString"];
DataTable dt =
TMG.Data.DataAccess.GetResultTable("upDisplaySites", connstring);
DataTable dt1 =
TMG.Data.DataAccess.GetResultTable("upDisplayGroups", connstring);
DataTable dt2 =
TMG.Data.DataAccess.GetResultTable("upDisplayUsers", connstring);
treeView1.Nodes.Clear();
try
{
TreeNode parentNode = new TreeNode();
foreach(DataRow dr in dt.Rows)
{
treeView1.Nodes.Add(dr["Name"].ToString());
//TreeNode tNode = new TreeNode();
parentNode = treeView1.Nodes[0];
}
// this needs to be changed!
foreach(DataRow dr1 in dt1.Rows)
{
TreeNode subNode = new TreeNode();
parentNode.Nodes.Add(dr1["Name"].ToString());
subNode = treeView1.Nodes[0];
}
}
catch (Exception ex){ MessageBox.Show("Err");}
}
}
//--------------------------------------------------------------------------------------
the above displayed the Sites as follows:
==============
-a
-1
-2
-3
-b
-c
-d
-f
==============
The idea is to display the sites then the groups within the sites and
then the users in each group. (i.e. 3 levels of nodes)
Any idea of I could logically work this out. (note: the table are as
follows: site, group, user and others involved. i.e. multiple table
are used)
Thank you in advance.
Kam