Skip to content

Treeview in visual studio 2012

Devendra Dighe

Asked Feb 5, 2014, 9:08 PM EST2 answers
Share
LinkedInX
hello, I am using trying to use treeview in visual studio 2012 asp.net4.5,c#, and MySql Database. I took treeview control from toolbox and populated it.
But it does not show any output. it shows blank screen
my database contains two tables table i.e  subject(subject_id,subject_name)  & topic(topic_id,subject_id,topics)
here is my c# code:
 public partial class Study : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
}
private void PopulateRootLevel()
{
try
{
MySqlConnection con = new MySqlConnection(@"User Id=root;Host=localhost;Database=helix");
MySqlCommand cmd = new MySqlCommand(@"SELECT topic_id,topics,(SELECT count(*) FROM topic WHERE subject_id =sc.topic_id) childnodecount FROM topic sc WHERE subject_id IS NULL ", con);
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void PopulateSublevel(int subject_id, TreeNode parentNode)
{
try
{
MySqlConnection con = new MySqlConnection(@"User Id=root;Host=localhost;Database=helix");
MySqlCommand cmd = new MySqlCommand(@"SELECT topic_id,topics,(SELECT count(*) FROM topic WHERE subject_id=sc.topic_id) childnodecount FROM topic sc WHERE subject_id=@subject_id ", con);
con.Open();
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = subject_id;
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
con.Close();
}
catch(Exception ex)
{
throw ex;
}
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSublevel(Int32.Parse(e.Node.Value), e.Node);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["topics"].ToString();
tn.Value = dr["topic_id"].ToString();
nodes.Add(tn);
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
}
}
}
 and .aspx code for tree view
<asp:TreeView
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
runat="server"
OnTreeNodePopulate="TreeView1_TreeNodePopulate" />

It shows blank screen in output .plz help me . thank you 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    The TreeView Control for Windows Forms is designed for developers to create complicated navigation systems for Windows Forms applications and easily and fast display hierarchical structures. The WinForms TreeView Control offers a powerful Data Binding support, drag and drop items capability. When you apply the TreeView Control to your Windows Forms application, it will deeply improve the navigation and the UI design to your application. The TreeView for Windows Forms provides excellent user experience through the use of virtualization and is able to load thousands of nodes in milliseconds.In addition, TreeView provides effortless skinning and ready to use themes. I got those info from a C# UI Component site. You may like to do more investigations on it.
    0