从数据库绑定C#Windows窗体TreeView [英] Bind C# Windows Form TreeView from DataBase
本文介绍了从数据库绑定C#Windows窗体TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库表结构是这样的:
My DataBase Table Structure is like this :
ID(Primary Key),Title(Nvarchar),ParentID(这是ID当前表的数据)
ID(Primary Key ) , Title (Nvarchar) , ParentID (this is the ID of current table)
,第一个节点(根)的 ParentID的值= -1
i加载了该数据在内存中。(例如,在类列表中)
如何将带有循环或其他内容的项添加到TreeView中?
and the 'ParentID' of first node (root) is valued = -1 i loaded this data in Memory.(for example in a list of class) how can i add the items with a loop or something else to TreeView ?
推荐答案
我确实写了这段代码:
private void Heading_Load(object sender, System.EventArgs e)
{
InsertNodes(null, 0);
}
private void InsertNodes(TreeNode n, int hdrID)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Acounting;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT hdrid,title FROM [Heading] WHERE ParentID=" + hdrID, con);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) {
TreeNode t = new TreeNode(rdr("title").ToString());
InsertNodes(t, Convert.ToInt16(rdr("hdrID").ToString()));
if (n == null) {
trvHeader.Nodes.Add(t);
} else {
n.Nodes.Add(t);
}
}
rdr.Close();
}
怎么样?
这篇关于从数据库绑定C#Windows窗体TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文