开发日记: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无限级分类(灵活控制显示形式)

转载于:https://www.cnblogs.com/luomingui/archive/2011/03/22/1982363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值