C#里的treeview控件如何能绑定数据库里的数据
我做的是应用程序,窗体里拖放一个TreeView控件,想把数据库里的数据在这个控件里显示出来,对不起我没有分数了,但是请高手们给我解答一下吧我很急我的TREEVIEW控件...
我做的是应用程序,窗体里拖放一个TreeView控件,想把数据库里的数据在这个控件里显示出来,对不起我没有分数了,但是请高手们给我解答一下吧 我很急
我的TREEVIEW控件是Winfrom窗体里的 不是ASP.NET里的TREEVIEW 展开
我的TREEVIEW控件是Winfrom窗体里的 不是ASP.NET里的TREEVIEW 展开
展开全部
数据表:
CREATE TABLE CateTable (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CateName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[RootID] [int] NOT NULL ,
[ParentID] [int] NOT NULL
)
GO
存储过程:
CREATE PROCEDURE CateTable_GetList AS
BEGIN
Select
CateID,
CateName,
RootID,
ParentID
From
CateTable
Order By
CateID,RootID,ParentID
END
代码:
private void MainForm_Load(object sender, EventArgs e)
{
//从数据库中读取数据
SqlConnection con = new SqlConnection(ConnString.ConStr);
SqlCommand cmd = new SqlCommand("CateTable_GetList", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
}
catch
{
}
finally
{
cmd = null;
con.Close();
}
//往TreeView中添加树节点
//添加根节点
TreeNode tn = new TreeNode();
tn.Text = "种类";
tn.Name = "0";//Name作为ID
tn.Tag = "0";//Tag作为RootID
tn.ImageIndex = 0;
tn.SelectedImageIndex = 0;
tv.Nodes.Add(tn);//该TreeView命名为tv
tv.SelectedNode = tv.TopNode;
//把其他节点加上去
if (ds != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
tn = new TreeNode();
tn.Text = dr["CateName"].ToString();
tn.Name = dr["CateID"].ToString();//Name作为CateID
tn.Tag = dr["RootID"].ToString();//Tag作为RootID
tn.ImageIndex = 1;
tn.SelectedImageIndex = 1;
//判断是否为主节点
if (dr["CateID"].ToString() == dr["RootID"].ToString())
{
//主节点
tv.SelectedNode = tv.TopNode;
}
else
{
//其他节点
if (tv.SelectedNode.Name != dr["ParentID"].ToString())
{
TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true);
if (tn_temp.Length > 0)
{
tv.SelectedNode = tn_temp[0];
}
}
}
tv.SelectedNode.Nodes.Add(tn);
}
tv.ExpandAll();//展开TreeView
tv.SelectedNode = tv.TopNode;
}
}
CREATE TABLE CateTable (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CateName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[RootID] [int] NOT NULL ,
[ParentID] [int] NOT NULL
)
GO
存储过程:
CREATE PROCEDURE CateTable_GetList AS
BEGIN
Select
CateID,
CateName,
RootID,
ParentID
From
CateTable
Order By
CateID,RootID,ParentID
END
代码:
private void MainForm_Load(object sender, EventArgs e)
{
//从数据库中读取数据
SqlConnection con = new SqlConnection(ConnString.ConStr);
SqlCommand cmd = new SqlCommand("CateTable_GetList", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
}
catch
{
}
finally
{
cmd = null;
con.Close();
}
//往TreeView中添加树节点
//添加根节点
TreeNode tn = new TreeNode();
tn.Text = "种类";
tn.Name = "0";//Name作为ID
tn.Tag = "0";//Tag作为RootID
tn.ImageIndex = 0;
tn.SelectedImageIndex = 0;
tv.Nodes.Add(tn);//该TreeView命名为tv
tv.SelectedNode = tv.TopNode;
//把其他节点加上去
if (ds != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
tn = new TreeNode();
tn.Text = dr["CateName"].ToString();
tn.Name = dr["CateID"].ToString();//Name作为CateID
tn.Tag = dr["RootID"].ToString();//Tag作为RootID
tn.ImageIndex = 1;
tn.SelectedImageIndex = 1;
//判断是否为主节点
if (dr["CateID"].ToString() == dr["RootID"].ToString())
{
//主节点
tv.SelectedNode = tv.TopNode;
}
else
{
//其他节点
if (tv.SelectedNode.Name != dr["ParentID"].ToString())
{
TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true);
if (tn_temp.Length > 0)
{
tv.SelectedNode = tn_temp[0];
}
}
}
tv.SelectedNode.Nodes.Add(tn);
}
tv.ExpandAll();//展开TreeView
tv.SelectedNode = tv.TopNode;
}
}
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
public partial class tree : System.Web.UI.Page
{
string connstrtree1 = System.Configuration.ConfigurationSettings.AppSettings["connstrtree1"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateTreeView();
}
}
/// <summary>
/// 创建 TreeView
/// 此方法采取一次性加载目标数据到 DataTable,递归查询 DataTable ,创建子节点
/// </summary>
private void CreateTreeView()
{
//const int VirtualRootId = 1;
// 虚拟的根节点 ID, 表中 ParentID=0 的节点表示无实际父节点
const string SQL_SELECT = "SELECT * FROM LSWLLB ";
//string connStr = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("DEMO15_TreeViewDemo.mdb"); ;
//OdbcDataAdapter
OdbcDataAdapter da = new OdbcDataAdapter(SQL_SELECT, connstrtree1);
DataTable dt = new DataTable();
da.Fill(dt);
string lbbh=(string)dt.Rows[0]["LSWLLB_LBBH"];
int i=Convert.ToInt16(lbbh.Length.ToString());
//GridView1.DataSource=dt;
//GridView1.DataBind();
CreateTreeViewRecursive(TreeView1.Nodes, dt, 1,"",i);
}
/// <summary>
/// 递归查询数据,创建 TreeNode 节点
/// </summary>
/// <param name="nodes"></param>
/// <param name="dataSource"></param>
/// <param name="parentId"></param>
private void CreateTreeViewRecursive(TreeNodeCollection nodes, DataTable dataSource, int parentId, string parent,int j)
{
string js,js1;
string fliter;
if (parent == "" || parent == null || parent.Length <= 0)
{
fliter = String.Format("LSWLLB_JS={0}", parentId);
}
else
{
fliter = String.Format("LSWLLB_JS={0} and LSWLLB_LBBH like '{1}%'", parentId, parent);
}
// 查询子节点
DataRow[] drArr = dataSource.Select(fliter);
TreeNode node;
foreach (DataRow dr in drArr)
{
js = dr["LSWLLB_LBBH"].ToString();
int len1 = Convert.ToInt16(js.Length.ToString());
//js2=js.Substring(0,len1);
node = new TreeNode();
nodes.Add(node);
node.Text = (string)dr["LSWLLB_LBMC"];
node.Value = (string)dr["LSWLLB_LBMC"];
node.Expanded = false;
// 递归创建子节点
if (dr["LSWLLB_MX"].ToString() == "0")
{
int len = Convert.ToInt16(js.Length.ToString()) / j + 1;
js1 = dr["LSWLLB_LBBH"].ToString();
CreateTreeViewRecursive(node.ChildNodes, dataSource, len,js1,j);
}
// 移除已添加行,提高性能
dataSource.Rows.Remove(dr);
}
}
}
直接用数据库里的数据的话,这个是的自己写代码的.
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
public partial class tree : System.Web.UI.Page
{
string connstrtree1 = System.Configuration.ConfigurationSettings.AppSettings["connstrtree1"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateTreeView();
}
}
/// <summary>
/// 创建 TreeView
/// 此方法采取一次性加载目标数据到 DataTable,递归查询 DataTable ,创建子节点
/// </summary>
private void CreateTreeView()
{
//const int VirtualRootId = 1;
// 虚拟的根节点 ID, 表中 ParentID=0 的节点表示无实际父节点
const string SQL_SELECT = "SELECT * FROM LSWLLB ";
//string connStr = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("DEMO15_TreeViewDemo.mdb"); ;
//OdbcDataAdapter
OdbcDataAdapter da = new OdbcDataAdapter(SQL_SELECT, connstrtree1);
DataTable dt = new DataTable();
da.Fill(dt);
string lbbh=(string)dt.Rows[0]["LSWLLB_LBBH"];
int i=Convert.ToInt16(lbbh.Length.ToString());
//GridView1.DataSource=dt;
//GridView1.DataBind();
CreateTreeViewRecursive(TreeView1.Nodes, dt, 1,"",i);
}
/// <summary>
/// 递归查询数据,创建 TreeNode 节点
/// </summary>
/// <param name="nodes"></param>
/// <param name="dataSource"></param>
/// <param name="parentId"></param>
private void CreateTreeViewRecursive(TreeNodeCollection nodes, DataTable dataSource, int parentId, string parent,int j)
{
string js,js1;
string fliter;
if (parent == "" || parent == null || parent.Length <= 0)
{
fliter = String.Format("LSWLLB_JS={0}", parentId);
}
else
{
fliter = String.Format("LSWLLB_JS={0} and LSWLLB_LBBH like '{1}%'", parentId, parent);
}
// 查询子节点
DataRow[] drArr = dataSource.Select(fliter);
TreeNode node;
foreach (DataRow dr in drArr)
{
js = dr["LSWLLB_LBBH"].ToString();
int len1 = Convert.ToInt16(js.Length.ToString());
//js2=js.Substring(0,len1);
node = new TreeNode();
nodes.Add(node);
node.Text = (string)dr["LSWLLB_LBMC"];
node.Value = (string)dr["LSWLLB_LBMC"];
node.Expanded = false;
// 递归创建子节点
if (dr["LSWLLB_MX"].ToString() == "0")
{
int len = Convert.ToInt16(js.Length.ToString()) / j + 1;
js1 = dr["LSWLLB_LBBH"].ToString();
CreateTreeViewRecursive(node.ChildNodes, dataSource, len,js1,j);
}
// 移除已添加行,提高性能
dataSource.Rows.Remove(dr);
}
}
}
直接用数据库里的数据的话,这个是的自己写代码的.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询