首先,窗体一加载,绑定省份和城市数据
//窗口加载事件 private void register_Load(object sender, EventArgs e) { bindPro();//绑定省份数据函数 } /// <summary> /// 绑定省份数据函数 /// </summary> public void bindPro() { //获取数据库数据 List<ProvinceData> list = new ProvinceDA().Select(); //添加到窗口中 if (list != null) { foreach (ProvinceData data in list) { comboPro.Items.Add(data.ProName); } comboPro.SelectedItem = comboPro.Items[0]; } } public void bindCity() { comboCity.Items.Clear(); //根据选取的省份获取省份编号 string proName=comboPro.SelectedItem.ToString(); string proCode = new ProvinceDA().SelectByProName(proName).ProCode; //根据选取的省份编号绑定城市数据 List<CityData>list = new CityDA().SelectByProCode(proCode); if (list != null) { foreach (CityData data in list) { comboCity.Items.Add(data.CityName); } comboCity.SelectedItem = comboCity.Items[0]; } }
给ComboBox控件添加一个“内容改变”触发事件
双击,追加委托
//根据选取省份的变化,变化城市 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { bindCity(); }
运行结果
时间: 2024-10-23 05:36:00