Go to my github

开发日记:C# : 对话框和System.Windows.Forms.TreeView 多级数据绑定

收集 C# Dialog 以备以后使用。

1: FolderBrowserDialog 类 目标路径

效果图:

代码:

string pfxfile = "";
FolderBrowserDialog dialog 
= new FolderBrowserDialog();
if (dialog.ShowDialog(this== DialogResult.OK)
{
    pfxfile 
= dialog.SelectedPath;
}

 2:OpenFileDialog类 选择文件

 

string pfxfile = "";
OpenFileDialog fileDialog1 
= new OpenFileDialog();
fileDialog1.Filter 
= "文件类型(*.xml,*.txt,*.config)|*.xml;*.txt;*.config|All files (*.*)|*.*";
fileDialog1.FilterIndex 
= 1;
fileDialog1.RestoreDirectory 
= true;
if (fileDialog1.ShowDialog()==DialogResult.OK)
{
  pfxfile 
= fileDialog1.FileName;
}

 

TreeView


 

效果图:

image

数据库结构:

image

主要代码:

 
01    /// <summary>  

02     /// 获取地市列表  

03      /// </summary>  

04     /// <returns></returns>  

05     public void GetPostCode(System.Windows.Forms.TreeView treeView1)  

06     {  

07         DataTable table = GetPostCode();  

08         //加主结点的子结点,调用递归方法  

09         DataView dv = new DataView(table);  

10         if ((dv.RowFilter = "ParentID = 0").Length > 0){  

11             foreach (DataRowView row in dv){  

12                 if (row["ParentID"].ToString() == "0"){  

13                     TreeNode node = new TreeNode();  

14                     node.Text = row["City"].ToString();  

15                     node.Tag = row["PostCode"].ToString();  

16                     treeView1.Nodes[0].Nodes.Add(node);  

17                     GetRight(node, dv);  

18                 }  

19             }  

20         }  

21     }  

22     private void GetRight(TreeNode node, DataView dv)  

23     {  

24         string right = node.Tag.ToString();  

25         if ((dv.RowFilter = "ParentID = " + right).Length > 0)  

26         {  

27             foreach (DataRowView row in dv)  

28             {  

29                 if (row["ParentID"].ToString() == right)  

30                 {  

31                     TreeNode newNode = new TreeNode();  

32                     newNode.Text = row["City"].ToString();  

33                     newNode.Tag = row["PostCode"].ToString();  

34                     node.Nodes.Add(newNode);  

35                     GetRight(newNode, dv);  

36                 }  

37             }  

38         }  

39     } 
调用代码:

GetPostCode(this.treeView1);

相关资源列表:

笔记:DropDownList无限级分类(灵活控制显示形式)

posted @ 2011-03-22 19:11  峡谷少爷  阅读(892)  评论(0编辑  收藏  举报