visual studio 2012中的简单树视图控件 [英] simple treeview control in visual studio 2012

查看:73
本文介绍了visual studio 2012中的简单树视图控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试在visual studio 2012 asp.net4.5,c#和MySql Database中使用treeview。我从工具箱中取出了treeview控件并填充它。

但它没有显示任何输出。它显示空白屏幕

我的数据库包含两个表格,即主题(subject_id,subject_name)&主题(topic_id,subject_id,主题)

它显示空白输出。没有错误.plz帮助我。

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)
It shows blank output.there is no error.plz help me with this.

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);
}
}
}




<asp:TreeView

ID="TreeView1"

ExpandDepth="0"

PopulateNodesFromClient="true"

ShowLines="true"

ShowExpandCollapse="true"

runat="server"

OnTreeNodePopulate="TreeView1_TreeNodePopulate" />

推荐答案

这可能对你有帮助..



http://stackoverflow.com/questions/361661/populate-treeview-from-database [ ^ ]
this might help you..

http://stackoverflow.com/questions/361661/populate-treeview-from-database[^]

这篇关于visual studio 2012中的简单树视图控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆