代码下载
TreeView无法绑定DATASET,所以在设计TreeView时要采用代码来实现树形列表的显示。如果要查询时则会根据TEXT属性的值来进行相关的查询。而无法根据主键或是自定义的值来进行查询。通过 TreeView的TAG(标签??)属性可以很好的解决这个问题。
树形列表加载代码:
1 public void LoadDataToTreeView()
2 {
3 if (LoadTest==false)
4 {
5 return;
6 }
7 tv.BeginUpdate ();
8 tv.Nodes.Clear ();
9 TreeNode root =new TreeNode ("餐饮公式",0,0);
10 tv.Nodes.Add (root);
11 DataTable dtTab;
12 dtTab=dtCy.Tables["tbltab"];
13 foreach (DataRow row in dtTab.Rows)
14 {
15 TreeNode ta=new TreeNode (row["IdName"].ToString(),1,2);
16 ta.Tag=row["Id"]; //将所需值加入tag
17 root.Nodes.Add (ta);
18 }
19 tv.EndUpdate ();
20 }
2 {
3 if (LoadTest==false)
4 {
5 return;
6 }
7 tv.BeginUpdate ();
8 tv.Nodes.Clear ();
9 TreeNode root =new TreeNode ("餐饮公式",0,0);
10 tv.Nodes.Add (root);
11 DataTable dtTab;
12 dtTab=dtCy.Tables["tbltab"];
13 foreach (DataRow row in dtTab.Rows)
14 {
15 TreeNode ta=new TreeNode (row["IdName"].ToString(),1,2);
16 ta.Tag=row["Id"]; //将所需值加入tag
17 root.Nodes.Add (ta);
18 }
19 tv.EndUpdate ();
20 }
根据TAG中的值进行相关查询:
1 private void tv_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
2 {
3
4 this.Cursor =Cursors.WaitCursor;
5 string CountInfo="";
6 dg.DataSource =null;
7 TreeNode tn=tv.SelectedNode ;
8
9 if (tn.ImageIndex ==0)
10 {
11 dv.Table =dtCy.Tables ["tbltab"];
12 CountInfo=
13 "共有餐饮公式大类"+dv.Table.Rows.Count +"个";
14 }
15 else if (tn.ImageIndex==1)
16 {
17 dv.Table=dtCy.Tables["Exp"];
18 dv.RowFilter ="IdNameId="+tn.Tag;
19 CountInfo=tn.Text +"类下共有记录"+dv.Count +"条";
20 dg.CaptionText ="餐饮公式一览表";
21 dg.DataSource=dv;
22 }
23
24 sb.Text =CountInfo;
25 this.Cursor =Cursors.Default ;
26 }
27
2 {
3
4 this.Cursor =Cursors.WaitCursor;
5 string CountInfo="";
6 dg.DataSource =null;
7 TreeNode tn=tv.SelectedNode ;
8
9 if (tn.ImageIndex ==0)
10 {
11 dv.Table =dtCy.Tables ["tbltab"];
12 CountInfo=
13 "共有餐饮公式大类"+dv.Table.Rows.Count +"个";
14 }
15 else if (tn.ImageIndex==1)
16 {
17 dv.Table=dtCy.Tables["Exp"];
18 dv.RowFilter ="IdNameId="+tn.Tag;
19 CountInfo=tn.Text +"类下共有记录"+dv.Count +"条";
20 dg.CaptionText ="餐饮公式一览表";
21 dg.DataSource=dv;
22 }
23
24 sb.Text =CountInfo;
25 this.Cursor =Cursors.Default ;
26 }
27
希望能通过这段代码来起到抛砖引玉的作用,如果您是高手请不要见笑