1、显示效果
2、数据insert脚本
insert into CITY(id,text,pid) values('1','城市',null)
insert into CITY(id,text,pid) values('2','北京市','1')
insert into CITY(id,text,pid) values('3','上海市','1')
insert into CITY(id,text,pid) values('4','天津市','1')
insert into CITY(id,text,pid) values('5','重庆市','1')
insert into CITY(id,text,pid) values('6','湖北省','1')
insert into CITY(id,text,pid) values('7','河北省','1')
insert into CITY(id,text,pid) values('8','山西省','1')
insert into CITY(id,text,pid) values('9','武汉市','6')
insert into CITY(id,text,pid) values('10','黄冈市','6')
insert into CITY(id,text,pid) values('11','石家庄','7')
insert into CITY(id,text,pid) values('12','唐山市','7')
insert into CITY(id,text,pid) values('13','太原市','8')
insert into CITY(id,text,pid) values('14','吉林省','1')
insert into CITY(id,text,pid) values('15','长春市','14')
insert into CITY(id,text,pid) values('16','南关区','15')
3、tvdemo.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
</td> </tr>
</table>
</div>
</form>
</body>
</html>
4、tvdemo.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; public partial class controlDemo_tvdemoaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ if(!IsPostBack)
{
//练习treview 静态手动添加节点
//TreeDemo(); //练习treview 动态递归添加节点
string sql = "select id,text,pid from CITY ";
DataClass dc = new DataClass();
TreeNode treen = null;
Bind_Tv(dc.GetDataTable(sql), treen, null, "id", "pid", "text"); //练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) )
//test();
} } #region 练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) )
public void test() {
DataClass dc = new DataClass();
TreeView TreeView1 = new TreeView(); string sql = "select id,text,pid from CITY ";
DataTable dt = new DataTable();
dt = dc.GetDataTable(sql); DataView dv = new DataView(dt); int lin = dv.Count; string filter = " pid = '1'";
dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据 int lin2 = dv.Count;
//foreach (DataRowView row in dv)
//{
// row["Item_ID"].ToString().Trim();
// row["Item_Name"].ToString().Trim();
//}
}
#endregion 练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) ) #region treview 静态手动添加节点 public void TreeDemo()
{
TreeNode tn = new TreeNode();
//tn.Value = "0";
tn.Text = "NBA";
TreeView1.Nodes.Add(tn); TreeNode hn = new TreeNode();
hn.Text = "Hoston";
tn.ChildNodes.Add(hn); TreeNode hpn = new TreeNode();
hpn.Text = "YaoMing";
hn.ChildNodes.Add(hpn); TreeNode ln = new TreeNode();
ln.Text = "Laker";
tn.ChildNodes.Add(ln); }
# endregion treview 静态手动添加节点 #region treview 动态递归添加节点
/// <summary>
/// 绑定TreeView(利用TreeNode)
/// </summary>
/// <param name="p_Node">TreeNode(TreeView的一个节点)</param>
/// <param name="pid_val">父id的值</param>
/// <param name="id">数据库 id 字段名</param>
/// <param name="pid">数据库 父id 字段名</param>
/// <param name="text">数据库 文本 字段值</param>
public void Bind_Tv(DataTable dt, TreeNode p_Node, string pid_val, string id, string pid, string text)
{
DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据
TreeNode tn;//建立TreeView的节点(TreeNode),以便将取出的数据添加到节点中
//以下为三元运算符,如果父id为空,则为构建“父id字段 is null”的查询条件,否则构建“父id字段=父id字段值”的查询条件
string filter = string.IsNullOrEmpty(pid_val) ? pid + " is null" : string.Format(pid + "='{0}'", pid_val);
dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据
//int lin = dv.Count; foreach (DataRowView row in dv)
{
tn = new TreeNode();//建立一个新节点(学名叫:一个实例)
if (p_Node == null)//如果为根节点
{
tn.Value = row[id].ToString().Replace(" ", "");//节点的Value值,一般为数据库的id值
tn.Text = row[text].ToString().Replace(" ", "");//节点的Text,节点的文本显示
TreeView1.Nodes.Add(tn);//将该节点加入到TreeView中
Bind_Tv(dt, tn, tn.Value, id, pid, text);//递归(反复调用这个方法,直到把数据取完为止)
//tn.Value除了根节点以后就是有值了!!!
}
else//如果不是根节点
{
tn.Value = row[id].ToString().Replace(" ", "");//节点Value值
tn.Text = row[text].ToString().Replace(" ", "");//节点Text值
p_Node.ChildNodes.Add(tn);//该节点加入到上级节点中
Bind_Tv(dt, tn, tn.Value, id, pid, text);//递归
}
}
} #endregion treview 动态递归添加节点
}
5、GetDataTable 返回Datatable方法
public DataTable GetDataTable(string sql)
{
this.OpenConnection();
this.cmd = new SqlCommand(sql, this.conn);
this.sdap = new SqlDataAdapter(this.cmd);
this.ds = new DataSet();
sdap.Fill(ds);
newdt = ds.Tables[]; return newdt;
}