完整的地区查询代码
1 private void lbProv_Loaded(object sender, RoutedEventArgs e) 2 { 3 List<Area> Prov = new List<Area>(); 4 DataTable table = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid=0"); 5 foreach (DataRow row in table.Rows) 6 { 7 Area prov = new Area(); 8 prov.areaId = (int)row["AreaId"]; 9 prov.areaName=(string)row["AreaName"]; 10 Prov.Add(prov); 11 } 12 13 lbProv.ItemsSource = Prov;//前台xaml里要设置DisplayMemberPath="areaName" 14 } 15 16 private void lbProv_SelectionChanged(object sender, SelectionChangedEventArgs e) 17 { 18 List<Area> City = new List<Area>(); 19 Area p=(Area)lbProv.SelectedItem; 20 DataTable table = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid=@Id", 21 new SqlParameter("@Id", Convert.ToInt32(p.areaId))); 22 foreach (DataRow row in table.Rows) 23 { 24 Area city = new Area(); 25 city.areaId = (int)row["AreaId"]; 26 city.areaName = (string)row["AreaName"]; 27 City.Add(city); 28 } 29 lbCity.ItemsSource = City; 30 } 31 32 private void lbCity_SelectionChanged(object sender, SelectionChangedEventArgs e) 33 { 34 List<Area> Area = new List<Area>(); 35 if (lbCity.SelectedItem != null)//注意这个判断 36 { 37 Area a = (Area)lbCity.SelectedItem; 38 DataTable table = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid=@Id", 39 new SqlParameter("@Id", Convert.ToInt32(a.areaId))); 40 foreach (DataRow row in table.Rows) 41 { 42 Area city = new Area(); 43 city.areaId = (int)row["AreaId"]; 44 city.areaName = (string)row["AreaName"]; 45 Area.Add(city); 46 } 47 lbArea.ItemsSource = Area; 48 } 49 50 }
我是一个小小虫
用编程改变世界