您好,登錄后才能下訂單哦!
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TreeViewList
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string conString = "Data Source=服務器名稱;Initial Catalog=數(shù)據(jù)庫名;User ID=登陸名;Pwd=密碼";
SqlConnection con = new SqlConnection(conString);
con.Open();
string strSql = "select * from 數(shù)據(jù)庫表名";
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
da.Fill(dt);
AddTreeNode(treeView1,0,null);
}
/// <summary>
/// 樹形TreeView綁定數(shù)據(jù)
/// </summary>
/// <param name="tv"></param>
/// <param name="parentid"></param>
/// <param name="pNode"></param>
protected void AddTreeNode(TreeView tv,int parentid, TreeNode pNode)
{
foreach (DataRow dv in dt.Select("parentID="+parentid))
{
TreeNode node = new TreeNode();
node.Text = dv["Name"].ToString();
node.Tag = dv["ID"].ToString();
if (pNode == null)
{
tv.Nodes.Add(node);
}
else
{
pNode.Nodes.Add(node);
}
AddTreeNode(tv, Convert.ToInt32(dv["ID"].ToString()), node);
}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。