新聞中心
using System;
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了建水免費建站歡迎大家使用!
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=服務(wù)器名稱;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);
}
///
/// 樹形TreeView綁定數(shù)據(jù)
///
///
///
///
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);
}
}
網(wǎng)站標(biāo)題:C#WinfromTreeview樹形結(jié)構(gòu)使用
URL地址:http://fisionsoft.com.cn/article/jcihop.html