treeview 递归绑定数据库中的数据 支持web winform_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > treeview 递归绑定数据库中的数据 支持web winform

treeview 递归绑定数据库中的数据 支持web winform

 2010/12/9 9:13:45  冷寒冰  http://pmandy-163-com.javaeye.com  我要评论(0)
  • 摘要:以下是完整代码附件中是数据表namespacetreeview{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}//窗体加载privatevoidForm1_Load(objectsender,EventArgse){InitGroupTree();}//得到初始化的树publicvoidInitGroupTree(){treeView1.Nodes.Clear()
  • 标签:for Web view 数据库 数据 winform 递归

以下是完整代码???? 附件中是数据表??

?

namespace treeview
{
??? public partial class Form1 : Form
??? {
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????? }
??????? //窗体加载
??????? private void Form1_Load(object sender, EventArgs e)
??????? {
??????????? InitGroupTree();

??????? }
??????? //得到初始化的树
?????? public void InitGroupTree()
??????? {
???????????
?????????? treeView1.Nodes.Clear();

?????????? GroupNode rootGroupNode =GetGroupNode("e1f6d5dd-941e-4300-9644-700bc96a24f4");

??????????? TreeNode node = LoadTreeNode(rootGroupNode);

?????????? treeView1.Nodes.Add(node);

??????????? InitSubGroupTree(node, rootGroupNode);

??????????? node.Expand();
??????? }

?????
??????? private TreeNode LoadTreeNode(GroupNode childGroupNode)
??????? {

??????????? TreeNode childNode = new TreeNode();
??????????? childNode.Text = childGroupNode.GroupDO.GroupName;
??????????? childNode.Tag = childGroupNode.GroupDO.GroupId.ToString();
?????????
??????????? //childNode.ExpandAll();

??????????? return childNode;
??????? }

??????? private void InitSubGroupTree(TreeNode node, GroupNode groupNode)
??????? {
??????????? foreach (var childGroupNode in groupNode.ChildGroups)
??????????? {
??????????????? TreeNode childNode = LoadTreeNode(childGroupNode);

?????????????? // node.ChildNodes.Add(childNode); web中
??????????????? node.Nodes.Add(childNode);//winform中
??????????????? InitSubGroupTree(childNode, childGroupNode);
??????????? }
??????? }

??????? public GroupNode GetGroupNode(string groupId)
??????? {
??????????? GroupDO groupDO = GetRootNodeDO(groupId);
??????????? if (groupDO == null)
??????????? {
??????????????? return null;
??????????? }

??????????? GroupNode groupNode = new GroupNode();
??????????? groupNode.GroupDO = groupDO;//得到根节点 (最顶级节点)


??????????? List<string> childGroupIds = GetChildGroupIds(groupId);
??????????? foreach (string childGroupId in childGroupIds)
??????????? {
??????????????? groupNode.ChildGroups.Add(GetGroupNode(childGroupId));
??????????? }

??????????? return groupNode;
??????? }


??????? /// <summary>
??????? /// Get the group by the group ID.
??????? /// </summary>
??????? /// <param name="groupId">The group ID.</param>
??????? /// <returns>The GroupDO object.</returns>
??????? public GroupDO GetRootNodeDO(string groupId)
??????? {
??????????? string constring = "Data Source=.;Initial Catalog=CIMSystemDB;User ID=sa;password=123;";
??????????? string sql = "SELECT [GroupId],[GroupName],[Description],[GroupMgrId],[SuperGroupId] FROM [Group] WHERE [GroupId]=@GroupId";
??????????? SqlConnection conn = new SqlConnection(constring);

??????????? try
??????????? {
??????????????? SqlParameter parameters = new SqlParameter("@GroupId", groupId);
??????????????? SqlCommand cmd = new SqlCommand(sql, conn);
??????????????? SqlDataAdapter da = new SqlDataAdapter();
??????????????? da.SelectCommand = cmd;
??????????????? cmd.Parameters.Add(parameters);

??????????????? DataSet ds = new DataSet();
??????????????? da.Fill(ds);
??????????????? DataTable dataTable = ds.Tables[0];

??????????????? GroupDO groupDO = null;

??????????????? if (dataTable != null && dataTable.Rows.Count > 0)
??????????????? {
??????????????????? groupDO = new GroupDO();
??????????????????? foreach (DataRow dr in dataTable.Rows)
??????????????????? {
??????????????????????? SetGroupDO(groupDO, dr);
??????????????????? }


??????????????? }

??????????????? return groupDO;
??????????? }
??????????? catch
??????????? {
??????????????? MessageBox.Show("发生异常");
??????????????? return null;
??????????? }
??????????? finally
??????????? {
??????????????? if (conn.State == ConnectionState.Open)
??????????????? {
??????????????????? conn.Close();
??????????????? }
??????????? }
??????? }
?????? public void SetGroupDO(GroupDO groupDO, DataRow dataRow)
??????? {
??????????? groupDO.GroupId =dataRow["GroupID"].ToString();
??????????? groupDO.GroupName =dataRow["GroupName"].ToString();
??????????? groupDO.Description = dataRow["Description"].ToString();
??????????? groupDO.GroupMgrId = dataRow["GroupMgrID"].ToString();
??????????? groupDO.SuperGroupId =dataRow["SuperGroupID"].ToString();
??????? }
?????

??????? /// <summary>
??????? /// Get one level child group ID list.
??????? /// </summary>
??????? /// <param name="groupId">The group ID.</param>
??????? /// <returns>The child group ID list.</returns>
??????? public static List<string> GetChildGroupIds(string groupId)
??????? {
??????????? string constring = "Data Source=.;Initial Catalog=CIMSystemDB;User ID=sa;password=123;";
??????????? SqlConnection conn=new SqlConnection(constring);
??????????? try
??????????? {
??????????????? string sql = "SELECT [GroupID] FROM [Group] WHERE [superGroupID]=@GroupId AND GroupID<>@GroupId AND [Actived]=1 "
??????????????????? + " ORDER BY [GroupName]";
??????????????? SqlParameter parameters = new SqlParameter("@GroupId", groupId);
??????????????? SqlCommand cmd = new SqlCommand(sql, conn);
??????????????? cmd.Parameters.Add(parameters);
??????????????? SqlDataAdapter da = new SqlDataAdapter();
??????????????? da.SelectCommand = cmd;
??????????????? DataSet ds = new DataSet();
??????????????? da.Fill(ds);
??????????????? DataTable dataTable =ds.Tables[0];
?????????????
??????????????? List<string> childGroupIds = new List<string>();

??????????????? if (dataTable != null)
??????????????? {
??????????????????? foreach (DataRow dataRow in dataTable.Rows)
??????????????????? {
??????????????????????? childGroupIds.Add(dataRow["groupID"].ToString());
??????????????????? }
??????????????? }

??????????????? return childGroupIds;
??????????? }
??????????? catch
??????????? {
??????????????? MessageBox.Show("发生异常");
??????????????? return null;
??????????? }
??????????? finally
??????????? {
??????????????? if (conn.State == ConnectionState.Open)
??????????????? {
??????????????????? conn.Close();
??????????????? }
??????????? }
??????? }

??? }
????
}

//用到的两个类文件如下:

?public class GroupDO
??? {

??????? public string GroupId { get; set; }
??????? public string GroupName { get; set; }
??????? public string Description { get; set; }
??????? public string GroupMgrId { get; set; }
??????? public string SuperGroupId { get; set; }
??? }

?

?

?

?

public? class GroupNode
??? {
??????? public GroupDO GroupDO {get;set; }
??????? public List<GroupNode> ChildGroups { get; set; }
??????? public GroupNode()
??????? {
??????????? ChildGroups = new List<GroupNode>();
??????? }
??? }

发表评论
用户名: 匿名