TreeView的省市選擇

來源:互聯網
上載者:User

一、父選擇視窗

二、子視窗提供選擇FrmSelectAreas

1、資料準備:

  在資料庫中有一張表Areas欄位為:AID,AName,APid;

  AID為欄位地區編號,AName為地區名稱,APid為地區父級AID.

2、FrmSelectAreas表單中,遞迴遍曆綁定資料

  

View Code

 1 void BindParent() //綁定父節點 2         { 3             //添加父節點 4             TreeNode parent = new TreeNode(); 5             parent.Text = "全國"; 6             parent.Tag = 0; 7             tvAreas.Nodes.Add(parent); 8             //擷取資料集 9            List<Areas> list = SQLHelper.GetALLAreas();10             //子節點綁定11            BindChildRen(parent,list);12            parent.Expand();//展開父節點13         }14         //子節點綁定15         void BindChildRen(TreeNode parent,List<Areas> list) 16         {17             //得到父節點的AId18             int Aid = Convert.ToInt32(parent.Tag);19             //遍曆20             foreach (Areas item in list)21             {22                 if (Aid == item.APid)23                 {24                     TreeNode tn = new TreeNode();25                     tn.Text = item.AName;26                     tn.Tag = item.AID;27                     parent.Nodes.Add(tn);28 29                     BindChildRen(tn,list);30                 }31             }32         }

 

 然後,在表單載入中BindParent();

2、擷取地區全名稱:

        void GetAreasText(TreeNode tn)         {            if (tn.Parent != null)             {                GetAreasText(tn.Parent);            }            strSelected += tn.Text + "-";  //strSelected為表單的public欄位,用於傳給父表單資料。        }

 3、單擊確認時,傳回資料,關閉當前表單

            GetAreasText(tvAreas.SelectedNode);//擷取資料            strSelected = strSelected.TrimEnd('-');            if (!string.IsNullOrEmpty(strSelected))            {                this.DialogResult = DialogResult.OK;            }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.