天道酬勤

博观而约取,厚积而薄发!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# treeView与数据库

Posted on 2010-04-29 18:57  Happy Coding  阅读(10468)  评论(0编辑  收藏  举报

思路:
我想做一个类似于 QQ 好友显示方式:(treeView)
一时无法解决几个问题:想请教!!!
首先:我已经创建好了一个分组表(Wait_Group)
(这表存于ECard数据库下,我声明一下我用的ACCESS数据库)

接下来:应该与 treeView1连接.可是不知道怎么连,,希望高手解决!!

而最终的效果应该是:(比如我现在Group表下
列名: G_id    groupname
         1    我的好友
         2    我的同学
         3    我的亲人 )
假如数据库有这几条信息,现在用treeView来显示信息
我要求的是:
          我的好友
                好友列表....
          我的同学
                好友列表....
          我的亲人
                好友列表....
注:看看QQ列表就知道了,我的要求是,用Wait_Group表出来的只能为上图示例,!!

1 using System;
2  using System.IO;
3 using System.Windows.Forms;
4 using System.Runtime.Serialization.Formatters.Binary;
5
6 namespace testAll
7 {
8 /// <summary>
9 /// TreeView串行化类
10 /// </summary>
11 public class TreeViewDataAccess
12 {
13 public TreeViewDataAccess(){}
14
15 /// <summary>
16 /// TreeViewData
17 /// </summary>
18 [Serializable()]
19 public struct TreeViewData
20 {
21 public TreeNodeData[] Nodes;
22
23 /// <summary>
24 /// 递归初始化TreeView数据
25 /// </summary>
26 /// <param name="treeview"></param>
27 public TreeViewData(TreeView treeview)
28 {
29 Nodes = new TreeNodeData[treeview.Nodes.Count];
30 if (treeview.Nodes.Count == 0)
31 {
32 return;
33 }
34 for (int i = 0; i <= treeview.Nodes.Count - 1; i++)
35 {
36 Nodes[i] = new TreeNodeData(treeview.Nodes[i]);
37 }
38 }
39
40 /// <summary>
41 /// 通过TreeViewData弹出TreeView
42 /// </summary>
43 /// <param name="treeview"></param>
44 public void PopulateTree(TreeView treeview)
45 {
46 if (this.Nodes == null || this.Nodes.Length == 0)
47 {
48 return;
49 }
50 treeview.BeginUpdate();
51 for (int i = 0; i <= this.Nodes.Length - 1; i++)
52 {
53 treeview.Nodes.Add(this.Nodes[i].ToTreeNode());
54 }
55 treeview.EndUpdate();
56 }
57 }
58
59 /// <summary>
60 /// TreeNodeData
61 /// </summary>
62 [Serializable()]
63 public struct TreeNodeData
64 {
65 public string Text;
66 public int ImageIndex;
67 public int SelectedImageIndex;
68 public bool Checked;
69 public bool Expanded;
70 public object Tag;
71 public TreeNodeData[] Nodes;
72
73 /// <summary>
74 /// TreeNode构造函数
75 /// </summary>
76 /// <param name="node"></param>
77 public TreeNodeData(TreeNode node)
78 {
79 this.Text = node.Text;
80 this.ImageIndex = node.ImageIndex;
81 this.SelectedImageIndex = node.SelectedImageIndex;
82 this.Checked = node.Checked;
83 this.Expanded = node.IsExpanded;
84 this.Nodes = new TreeNodeData[node.Nodes.Count];
85
86 if ((!(node.Tag == null)) && node.Tag.GetType().IsSerializable)
87 {
88 this.Tag = node.Tag;
89 }
90 else
91 {
92 this.Tag = null;
93 }
94 if (node.Nodes.Count == 0)
95 {
96 return;
97 }
98 for (int i = 0; i <= node.Nodes.Count - 1; i++)
99 {
100 Nodes[i] = new TreeNodeData(node.Nodes[i]);
101 }
102 }
103
104 /// <summary>
105 /// TreeNodeData返回TreeNode
106 /// </summary>
107 /// <returns></returns>
108 public TreeNode ToTreeNode()
109 {
110 TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
111 ToTreeNode.Checked = this.Checked;
112 ToTreeNode.Tag = this.Tag;
113 if (this.Expanded)
114 {
115 ToTreeNode.Expand();
116 }
117 if (this.Nodes == null && this.Nodes.Length == 0)
118 {
119 return null;
120 }
121 if(ToTreeNode != null && this.Nodes.Length == 0)
122 {
123 return ToTreeNode;
124 }
125 for (int i = 0; i <= this.Nodes.Length - 1; i++)
126 {
127 ToTreeNode.Nodes.Add(this.Nodes[i].ToTreeNode());
128 }
129 return ToTreeNode;
130 }
131 }
132 /// <summary>
133 /// 加载TreeView
134 /// </summary>
135 /// <param name="treeView"></param>
136 /// <param name="path"></param>
137 public static void LoadTreeViewData(TreeView treeView, string path)
138 {
139
140 BinaryFormatter ser = new BinaryFormatter();
141 Stream file = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
142 TreeViewData treeData = ((TreeViewData)(ser.Deserialize(file)));
143 treeData.PopulateTree(treeView);
144 file.Close();
145
146 }
147
148 /// <summary>
149 /// 保存TreeView到文件
150 /// </summary>
151 /// <param name="treeView"></param>
152 /// <param name="path"></param>
153 public static void SaveTreeViewData(TreeView treeView, string path)
154 {
155 BinaryFormatter ser = new BinaryFormatter();
156 Stream file = new FileStream(path,FileMode.Create);
157 ser.Serialize(file,new TreeViewData(treeView));
158 file.Close();
159
160 }
161 }
162
163
164 }
165