前几天,在。net QQ群里有人问到Treeview数据绑定,我很好奇,因为我从来没有听说过,treeview也能从数据库里绑定数据。。。。
小弟真是肤浅。。作为一个。net的菜鸟,我也只能虚心学习下。。。
下面是我自己通过查找资料后,自己做的一个treeview的数据绑定, 有什么意见大家提提,请大家涵涵.....
数据库连接字符串:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/**//// <summary>
///SQLconn 的摘要说明
/// </summary>
public class SQLconn
{
public SQLconn()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection createConn()
{
SqlConnection con = new SqlConnection(@"server=Jimmy\SQLEXPRESS;database=test;uid=sa;pwd=cxj7480326");
return con;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/**//// <summary>
///SQLconn 的摘要说明
/// </summary>
public class SQLconn
{
public SQLconn()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection createConn()
{
SqlConnection con = new SqlConnection(@"server=Jimmy\SQLEXPRESS;database=test;uid=sa;pwd=cxj7480326");
return con;
}
}
后台代码:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bindTreeview(TreeView1);
}
}
public void dataset(DataSet ds, string tablename, string sqlText)
{
SqlConnection con = SQLconn.createConn();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlText,con);
sda.Fill(ds, tablename);
}
public void bindTreeview(TreeView TV)
{
DataSet ProSet = new DataSet();
string sqlstr = "select * from Province";
dataset(ProSet,"Province",sqlstr);
if (ProSet.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ProSet.Tables[0].Rows.Count; i++)
{
TreeNode father = new TreeNode();
father.Value = ProSet.Tables[0].Rows[i]["ProId"].ToString();
father.Text = ProSet.Tables[0].Rows[i]["ProName"].ToString();
father.SelectAction = TreeNodeSelectAction.None;
DataSet ProCity = new DataSet();
sqlstr = "select * from City where ProId='"+ ProSet.Tables[0].Rows[i]["ProId"].ToString().Trim()+"'";
dataset(ProCity, "City", sqlstr);
if (ProCity.Tables[0].Rows.Count > 0)
{
for (int m = 0; m < ProCity.Tables[0].Rows.Count; m++)
{
TreeNode sun = new TreeNode();
sun.Value = ProCity.Tables[0].Rows[m]["CityId"].ToString();
sun.Text = ProCity.Tables[0].Rows[m]["CityName"].ToString();
sun.Selected = false;
sun.SelectAction = TreeNodeSelectAction.None;
father.ChildNodes.Add(sun);
}
}
father.ExpandAll();
TV.Nodes.Add(father);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bindTreeview(TreeView1);
}
}
public void dataset(DataSet ds, string tablename, string sqlText)
{
SqlConnection con = SQLconn.createConn();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlText,con);
sda.Fill(ds, tablename);
}
public void bindTreeview(TreeView TV)
{
DataSet ProSet = new DataSet();
string sqlstr = "select * from Province";
dataset(ProSet,"Province",sqlstr);
if (ProSet.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ProSet.Tables[0].Rows.Count; i++)
{
TreeNode father = new TreeNode();
father.Value = ProSet.Tables[0].Rows[i]["ProId"].ToString();
father.Text = ProSet.Tables[0].Rows[i]["ProName"].ToString();
father.SelectAction = TreeNodeSelectAction.None;
DataSet ProCity = new DataSet();
sqlstr = "select * from City where ProId='"+ ProSet.Tables[0].Rows[i]["ProId"].ToString().Trim()+"'";
dataset(ProCity, "City", sqlstr);
if (ProCity.Tables[0].Rows.Count > 0)
{
for (int m = 0; m < ProCity.Tables[0].Rows.Count; m++)
{
TreeNode sun = new TreeNode();
sun.Value = ProCity.Tables[0].Rows[m]["CityId"].ToString();
sun.Text = ProCity.Tables[0].Rows[m]["CityName"].ToString();
sun.Selected = false;
sun.SelectAction = TreeNodeSelectAction.None;
father.ChildNodes.Add(sun);
}
}
father.ExpandAll();
TV.Nodes.Add(father);
}
}
}
}
前台代码:
<
body
>
< form id = " form1 " runat = " server " >
< div >
< asp:TreeView ID = " TreeView1 " runat = " server " >
</ asp:TreeView >
</ div >
</ form >
</ body >
< form id = " form1 " runat = " server " >
< div >
< asp:TreeView ID = " TreeView1 " runat = " server " >
</ asp:TreeView >
</ div >
</ form >
</ body >
显示效果: