C#WinfromTreeview树形结构使用
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=数据库名;User ID=登陆名;Pwd=密码";
SqlConnection con = new SqlConnection(conString);
con.Open();
string strSql = "select * from 数据库表名";
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
da.Fill(dt);
AddTreeNode(treeView1,0,null);
}
///
/// 树形TreeView绑定数据
///
///
///
///
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);
}
}
分享题目:C#WinfromTreeview树形结构使用
标题网址:http://ybzwz.com/article/jcihop.html