C#中TreeView与数据库绑定?
递归方法。
//
/// TreeView数据绑定
///
///
protected void CreateTreeView()
{
TreeNode rootNode = new TreeNode();
rootNode.Text = "全部";
rootNode.Value = "-1";
BasicTypeTreeView.Nodes.Add(rootNode);
BasicInformationManager BasicManager = new BasicInformationManager();
var tlist = BasicManager.GetAllBasicInfoTypes();
foreach (Model.BasicInforType mtype in tlist)
{
if (mtype.ParentID == 0)
{
TreeNode Node = new TreeNode();
//添加根节点
Node.Text = mtype.BasicTypeName;
Node.Value = mtype.ID.ToString();
Node.ToolTip = "编码" + mtype.BasicCode;
getSubNode(ref Node, mtype.ID);
rootNode.ChildNodes.Add(Node);
}
}
}
///
/// 获取treeView子节点
///
///
///
public void getSubNode(ref TreeNode node, int item)
{
BasicInformationManager BasicManager = new BasicInformationManager();
var tlist = BasicManager.GetBasicInforTypeByParentId(item);
if (tlist.Count > 0)
{
foreach (Model.BasicInforType m in tlist)
{
TreeNode subNode = new TreeNode();
subNode.Text = m.BasicTypeName;
subNode.Value = m.ID.ToString();
subNode.ToolTip = "编码" + m.BasicCode;
getSubNode(ref subNode, m.ID);
node.ChildNodes.Add(subNode); //进行子节点循环添加
}
}
}
C#手动绑定treeview怎么写来着?
private void addTree(TreeNode tn, string dept)
{
DataTable dt = cf.getDataTable("select 部门名称 from 部门表 where 上级部门= + dept + ");
foreach (DataRow dr in dt.Rows)
{
TreeNode ctn = new TreeNode(dr[0].ToString());
ctn.SelectedImageIndex = 0;
ctn.ImageIndex = 0;
DataTable dtg = cf.getDataTable("select 岗位名称 from 岗位表 where 所属部门= + dr[0].ToString() + ");
foreach (DataRow drg in dtg.Rows)
{
TreeNode tng = new TreeNode(drg[0].ToString());
tng.SelectedImageIndex = 1;
tng.ImageIndex = 1;
ctn.Nodes.Add(tng);
}
tn.Nodes.Add(ctn);
addTree(ctn,dr[0].ToString());
}
}
private void FormManagerCenter_Load(object sender, EventArgs e)
{
TreeNode tn = new TreeNode("公司架构");
addTree(tn, "公司架构");
treeView1.Nodes.Add(tn);
}
利用递归原理
WinForm中数据绑定TreeView
这是一个绑定的方法,可以参考一下!
public void InitTvArea()
{
Area.Nodes.Clear();
TreeNode rootNode =Area.Nodes.Add("区域划分");
IList<Area> listArea = AreaManager.Get_Area_Father();
foreach (Area area in listArea)
{
TreeNode node = new TreeNode(area.AreaName); //根节点
Area areaid = AreaManager.Get_Area_idByName(area.AreaName);
IList<Area> listAreaChild = AreaManager.Get_Area_Childs(areaid.Id + "___");
if (listAreaChild.Count != 0)
{
foreach (Area areaChild in listAreaChild)
{
TreeNode childNode = new TreeNode(areaChild.AreaName); //子节点
node.Nodes.Add(childNode);
}
}
rootNode.Nodes.Add(node); //添加字节点
}
rootNode.Expand();
}
怎么用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.SelectedNode =.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 =.TopNode;
}
else
{
//其他节点
if .SelectedNode.Name != dr["ParentID"].ToString())
{
TreeNode[] tn_temp =.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 =.TopNode;
}
}