本帖最后由 y494890511 于 2013-11-18 13:33 编辑
- public partial class 省市县联动 : Window
- {
- public 省市县联动()
- {
- InitializeComponent();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- DataTable table = SqlHelper.ExecuteDataTable("select * from Data_Province");
- List<Area> list = new List<Area>();
- foreach(DataRow row in table.Rows)
- {
- Area area=new Area();
- area.Id=(string)row["ProvinceCode"];
- area.Name=(string)row["ProvinceName"];
- list.Add(area);
- }
- lbProv.ItemsSource=list;
- }
- private void lbProv_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- Area area=(Area)lbProv.SelectedItem;
- DataTable table=SqlHelper.ExecuteDataTable("select * from Data_City where ProvinceCode=@prov ",
- new SqlParameter("@prov",area.Id));
- List<Area> list=new List<Area>();
- foreach (DataRow row in table.Rows)
- {
- Area city=new Area();
- city.Id=(string)row["CityCode"];
- city.Name=(string)row["CityName"];
- list.Add(city);
- }
- lbCity.ItemsSource = list;
- }
- private void lbCity_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- Area area = (Area)lbCity.SelectedItem;
- DataTable table = SqlHelper.ExecuteDataTable("select * from Data_County where CityCode=@city",
- new SqlParameter("@city",area.Id));
- List<Area> list = new List<Area>();
- foreach (DataRow row in table.Rows)
- {
- Area county = new Area();
- county.Id = (string)row["CountyCode"];
- county.Name = (string)row["CountyName"];
- list.Add(county);
- }
- lbCounty.ItemsSource = list;
- }
- }
- }
复制代码 Area类- class Area
- {
- public string Id { get; set; }
- public string Name { get; set; }
- }
复制代码 1.窗体加载时在lbProv列表框显示省份,当lbProv_SelectionChanged事件触发是在lbCity列表框显示市,
当lbCity_SelectionChanged事件触发是在lbCounty显示县
2.当选择一个省份后在选择一个市,接着在选择一个省份,结果出错.
当再次选择一个省份时触发lbProv_SelectionChanged事件,该省包含的所有市改变触发lbCity_SelectionChanged事件结果出错.
3.请问怎么解决?
|
-
91.png
(24.52 KB, 下载次数: 5)
窗体
-
92.png
(58.74 KB, 下载次数: 2)
错误提示
-
93.png
(79.21 KB, 下载次数: 2)
Data_City市表
-
94.png
(68.58 KB, 下载次数: 3)
Data_County县表
-
95.png
(67.18 KB, 下载次数: 6)
Data_Prov省表
组图打开中,请稍候......
|