4个回答
展开全部
第一种填充时展开节点
TreeNode tempnd;
//定义一个数据集
public DataTable table = new DataTable();
//填充单元
public DataSet ds = new DataSet();
List<int> HFunid = new List<int>();
string SqlConStr = "server=.;uid=sa;pwd=;database=exampletest;";
string sqlstr = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<int> a = new List<int>();
a.Add(2);
HFunid = a;
filldata();
}
}
private void filldata()
{
SqlConnection con = new SqlConnection(SqlConStr);
sqlstr = "select id,name,parentid from filltree";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(ds);
InitTree(TreeView1.Nodes, "0");
}
//填充时展开节点
private void InitTree(TreeNodeCollection nds, string parent)
{
DataRow[] rows = ds.Tables[0].Select("parentid='" + parent + "'");
foreach (DataRow row in rows) //递归算法来实现树控件的动态填充
{
tempnd = new TreeNode();
tempnd.Value = row["id"].ToString();
tempnd.Text = row["name"].ToString();
//判断check是否为真
for (int i = 0; i < HFunid.Count; i++)
{
if (Convert.ToInt32(tempnd.Value) == HFunid[i])
{
tempnd.Checked = true;
}
}
nds.Add(tempnd);
InitTree(tempnd .ChildNodes, tempnd.Value);
}
}
//填充时不展开节点
DataSet ds = new DataSet();
string sqlstr = "";
string SqlConStr = "server=.;uid=sa;pwd=;database=exampletest;";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
filldata();
}
}
//填充tree
private void filldata()
{
SqlConnection con = new SqlConnection(SqlConStr);
sqlstr = "select id,name,parentid from filltree where parentid='0'";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(ds);
InitTree(TreeView1.Nodes, "0");
}
//加载时填充根节点,判断是否有子节点
private void InitTree(TreeNodeCollection nds, string parent)
{
SqlConnection con = new SqlConnection(SqlConStr);
TreeNode tempnd;
DataRow[] rows = ds.Tables[0].Select("parentid='" + parent + "'");
foreach (DataRow row in rows) //递归算法来实现树控件的动态填充
{
tempnd = new TreeNode();
tempnd.Value = row["id"].ToString();
tempnd.Text = row["name"].ToString();
sqlstr = "select id,name,parentid from filltree where parentid='" + row["id"].ToString() + "'";
SqlCommand com = new SqlCommand(sqlstr,con);
con.Open();
int count = 0;
if (com.ExecuteScalar() != null)
{
count = int.Parse(com.ExecuteScalar().ToString());
}
con.Close();
if (count > 0)
{
tempnd.PopulateOnDemand = true;//树前面的加号属性
tempnd.Expanded = false;//是否展开
}
nds.Add(tempnd);
//InitTree(tempnd.ChildNodes, tempnd.Value);
count = 0;
}
}
//加号展开事件
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
int depid = int.Parse(e.Node.Value);
SqlConnection con = new SqlConnection(SqlConStr);
if (depid != 0)
{
sqlstr = "select id,name,parentid from filltree where parentid='" + depid + "'";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = ds.Tables[0].Rows[i][1].ToString();
node.Value = ds.Tables[0].Rows[i][0].ToString();
if (ds.Tables[0].Rows.Count > 0)
{
node.PopulateOnDemand = true;
node.Expanded = false;
}
sqlstr = "select id,name,parentid from filltree where parentid='" + ds.Tables[0].Rows[i][0].ToString() + "'";
SqlCommand cmd = new SqlCommand(sqlstr, con);
con.Open();
if (cmd.ExecuteScalar() != null)
{
node.PopulateOnDemand = true;
node.Expanded = false;
}
else
node.PopulateOnDemand = false;
e.Node.ChildNodes.Add(node);
con.Close();
}
}
}
TreeNode tempnd;
//定义一个数据集
public DataTable table = new DataTable();
//填充单元
public DataSet ds = new DataSet();
List<int> HFunid = new List<int>();
string SqlConStr = "server=.;uid=sa;pwd=;database=exampletest;";
string sqlstr = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<int> a = new List<int>();
a.Add(2);
HFunid = a;
filldata();
}
}
private void filldata()
{
SqlConnection con = new SqlConnection(SqlConStr);
sqlstr = "select id,name,parentid from filltree";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(ds);
InitTree(TreeView1.Nodes, "0");
}
//填充时展开节点
private void InitTree(TreeNodeCollection nds, string parent)
{
DataRow[] rows = ds.Tables[0].Select("parentid='" + parent + "'");
foreach (DataRow row in rows) //递归算法来实现树控件的动态填充
{
tempnd = new TreeNode();
tempnd.Value = row["id"].ToString();
tempnd.Text = row["name"].ToString();
//判断check是否为真
for (int i = 0; i < HFunid.Count; i++)
{
if (Convert.ToInt32(tempnd.Value) == HFunid[i])
{
tempnd.Checked = true;
}
}
nds.Add(tempnd);
InitTree(tempnd .ChildNodes, tempnd.Value);
}
}
//填充时不展开节点
DataSet ds = new DataSet();
string sqlstr = "";
string SqlConStr = "server=.;uid=sa;pwd=;database=exampletest;";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
filldata();
}
}
//填充tree
private void filldata()
{
SqlConnection con = new SqlConnection(SqlConStr);
sqlstr = "select id,name,parentid from filltree where parentid='0'";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(ds);
InitTree(TreeView1.Nodes, "0");
}
//加载时填充根节点,判断是否有子节点
private void InitTree(TreeNodeCollection nds, string parent)
{
SqlConnection con = new SqlConnection(SqlConStr);
TreeNode tempnd;
DataRow[] rows = ds.Tables[0].Select("parentid='" + parent + "'");
foreach (DataRow row in rows) //递归算法来实现树控件的动态填充
{
tempnd = new TreeNode();
tempnd.Value = row["id"].ToString();
tempnd.Text = row["name"].ToString();
sqlstr = "select id,name,parentid from filltree where parentid='" + row["id"].ToString() + "'";
SqlCommand com = new SqlCommand(sqlstr,con);
con.Open();
int count = 0;
if (com.ExecuteScalar() != null)
{
count = int.Parse(com.ExecuteScalar().ToString());
}
con.Close();
if (count > 0)
{
tempnd.PopulateOnDemand = true;//树前面的加号属性
tempnd.Expanded = false;//是否展开
}
nds.Add(tempnd);
//InitTree(tempnd.ChildNodes, tempnd.Value);
count = 0;
}
}
//加号展开事件
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
int depid = int.Parse(e.Node.Value);
SqlConnection con = new SqlConnection(SqlConStr);
if (depid != 0)
{
sqlstr = "select id,name,parentid from filltree where parentid='" + depid + "'";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = ds.Tables[0].Rows[i][1].ToString();
node.Value = ds.Tables[0].Rows[i][0].ToString();
if (ds.Tables[0].Rows.Count > 0)
{
node.PopulateOnDemand = true;
node.Expanded = false;
}
sqlstr = "select id,name,parentid from filltree where parentid='" + ds.Tables[0].Rows[i][0].ToString() + "'";
SqlCommand cmd = new SqlCommand(sqlstr, con);
con.Open();
if (cmd.ExecuteScalar() != null)
{
node.PopulateOnDemand = true;
node.Expanded = false;
}
else
node.PopulateOnDemand = false;
e.Node.ChildNodes.Add(node);
con.Close();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没用递归
public static bool GetData()
{
mBoot = new TreeNode("参数化文件列表");
try
{
DataTableCollection dts = TlsHelper.GetTablesFormXml("tlscad.trv");
DataTable cdt = dts["Class"];
DataTable sdt = dts["SubClass"];
int m = cdt.Rows.Count;
for (int i = 0; i < m; i++)
{
DataRow cdr = cdt.Rows[i];
TreeNode cnode = new TreeNode(cdr["Name"].ToString());
DataRow[] sdrs = sdt.Select("cid=" + cdr["cid"].ToString());
int n = sdrs.Length;
for (int j = 0; j < n; j++)
{
DataRow sdr = sdrs[j];
TreeNode snode = new TreeNode(sdr["Name"].ToString());
snode.ImageIndex = 2;
snode.SelectedImageIndex = 3;
cnode.Nodes.Add(snode);
}
mBoot.Nodes.Add(cnode);
}
mBoot.ExpandAll();
return true;
}
catch
{
return false;
}
}//
public static bool GetData()
{
mBoot = new TreeNode("参数化文件列表");
try
{
DataTableCollection dts = TlsHelper.GetTablesFormXml("tlscad.trv");
DataTable cdt = dts["Class"];
DataTable sdt = dts["SubClass"];
int m = cdt.Rows.Count;
for (int i = 0; i < m; i++)
{
DataRow cdr = cdt.Rows[i];
TreeNode cnode = new TreeNode(cdr["Name"].ToString());
DataRow[] sdrs = sdt.Select("cid=" + cdr["cid"].ToString());
int n = sdrs.Length;
for (int j = 0; j < n; j++)
{
DataRow sdr = sdrs[j];
TreeNode snode = new TreeNode(sdr["Name"].ToString());
snode.ImageIndex = 2;
snode.SelectedImageIndex = 3;
cnode.Nodes.Add(snode);
}
mBoot.Nodes.Add(cnode);
}
mBoot.ExpandAll();
return true;
}
catch
{
return false;
}
}//
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WB_DB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(@"server=.;uid=sa;pwd=sa;database=WB_DB");
con.Open();
SqlDataAdapter dap = new SqlDataAdapter("select *from T_PKID ", con);
dap.Fill(dt);
con.Close();
DataRow[] dr = dt.Select("ID=0");
for (int j = 0; j < dr.Length; j++)
{
TreeNode tr = new TreeNode();
tr.Text = dr[j]["Name"].ToString();
tr.Name = dr[j]["PKID"].ToString();
AddTreeNode(dt, tr);
treeView1.Nodes.Add(tr);
}
}
public void AddTreeNode(DataTable dt, TreeNode tr)
{
DataRow[] dr = dt.Select("ID=" + tr.Name);
for (int i = 0; i < dr.Length; i++)
{
TreeNode trn = new TreeNode();
trn.Name = dr[i]["PKID"].ToString();
trn.Text = dr[i]["Name"].ToString();
AddTreeNode(dt, trn);
tr.Nodes.Add(trn);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WB_DB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(@"server=.;uid=sa;pwd=sa;database=WB_DB");
con.Open();
SqlDataAdapter dap = new SqlDataAdapter("select *from T_PKID ", con);
dap.Fill(dt);
con.Close();
DataRow[] dr = dt.Select("ID=0");
for (int j = 0; j < dr.Length; j++)
{
TreeNode tr = new TreeNode();
tr.Text = dr[j]["Name"].ToString();
tr.Name = dr[j]["PKID"].ToString();
AddTreeNode(dt, tr);
treeView1.Nodes.Add(tr);
}
}
public void AddTreeNode(DataTable dt, TreeNode tr)
{
DataRow[] dr = dt.Select("ID=" + tr.Name);
for (int i = 0; i < dr.Length; i++)
{
TreeNode trn = new TreeNode();
trn.Name = dr[i]["PKID"].ToString();
trn.Text = dr[i]["Name"].ToString();
AddTreeNode(dt, trn);
tr.Nodes.Add(trn);
}
}
}
}
参考资料: 我自已写的!希望对你有帮肋 无限添加
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大致写一个,可能有语法错误:
public TreeNode LoadTreeNode(TreeNode TN)
{
//取数据库数据看有子项没有
DataTable DT = myDataset.GetChildDataByParentID(TN.Value);
if (DT.Count > 0)
{
for (int i = 0; i < DT.Rows.Count; i++)
{
TN.ChildNodes.Add(new TreeNode(DT.Rows[i]["Name"].ToString(), DT.Rows[i]["Value"].ToString()));
}
foreach (TreeNode tn in TN.ChildNodes)
{
LoadTreeNode(tn);
}
}
return TN;
}
public TreeNode LoadTreeNode(TreeNode TN)
{
//取数据库数据看有子项没有
DataTable DT = myDataset.GetChildDataByParentID(TN.Value);
if (DT.Count > 0)
{
for (int i = 0; i < DT.Rows.Count; i++)
{
TN.ChildNodes.Add(new TreeNode(DT.Rows[i]["Name"].ToString(), DT.Rows[i]["Value"].ToString()));
}
foreach (TreeNode tn in TN.ChildNodes)
{
LoadTreeNode(tn);
}
}
return TN;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询