C# winform 选择项 省市连动

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 省市联动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //加载省份信息到第一个ComboBox
            LoadProvince();

            //设置两个下拉菜单的默认值为“请选择”
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;

        }
        private void LoadProvince()
        {

            string sql = "select * from TblArea where AreaPid=0";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ProvinceItem item = new ProvinceItem();
                        item.AreaId = reader.GetInt32(0);
                        item.AreaName = reader.GetString(1);
                        item.AreaPid = reader.GetInt32(2);
                        comboBox1.Items.Add(item);
                    }
                }
            }
            //为ComboBox 增加一个“请选择”
            ProvinceItem itemDefault = new ProvinceItem();
            itemDefault.AreaId = -1;
            itemDefault.AreaName = "请选择";
            comboBox1.Items.Insert(0, itemDefault);

        }

        //下拉菜单的选择项改变事件
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取当前用户选择的项
            if (comboBox1.SelectedIndex > 0)
            {
                //加载第二个下拉菜单,数据来源:根据第一个下拉菜单用户选择项的AreaId来查询该项的所有子项。(子项:指的就是当前选中省份的下的直接城市)

                //获取当前选中项的id
                ProvinceItem item = comboBox1.SelectedItem as ProvinceItem;
                int areaId = item.AreaId;
                LoadCity(areaId);
            }
        }

        private void LoadCity(int areaId)
        {
            comboBox2.Items.Clear();
            string sql = "select * from TblArea where [email protected]";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@aid", areaId)))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ProvinceItem item = new ProvinceItem();
                        item.AreaId = reader.GetInt32(0);
                        item.AreaName = reader.GetString(1);
                        item.AreaPid = reader.GetInt32(2);
                        comboBox2.Items.Add(item);
                    }
                }
            }

            //也加一个【请选择】
            ProvinceItem itemDefault = new ProvinceItem();
            itemDefault.AreaId = -1;
            itemDefault.AreaName = "请选择";
            comboBox2.Items.Insert(0, itemDefault);
            comboBox2.SelectedIndex = 0;
        }
    }
    public class ProvinceItem
    {
        public int AreaId { get; set; }
        public string AreaName { get; set; }
        public int AreaPid { get; set; }
        public override string ToString()
        {
            return this.AreaName;
        }
    }
}
时间: 2024-08-03 00:18:27

C# winform 选择项 省市连动的相关文章

解决QML开发中ComboBox中一个已选择项没有清除的问题

解决QML开发中ComboBox中一个已选择项没有清除的问题 近期使用QML开发一个项目.须要使用ComboBox进行显示.当进行一个操作时,须要向ComboBox加入一个元素,当进行另外一个操作时.须要清除ComboBox里面的元素. 可是在操作的过程中,出现了一个诡异的现象--ComboBox里面的已选择项并没有清除. 以下是程序的截图,能够看到.ComboBox中已选择项并没有删除.可是ComboBox中的候选项已经删除了. 我在QTCN上进行提问.后面再大家的努力下,最终把这个问题攻克了

iOS开发——UI篇&下拉弹出列表选择项效果

下拉弹出列表选择项效果 右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上:弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod ‘LMDropdownView’:LMDropdownView是把想要的视图赋给它: 源代码地址:https://github.com/JxbSir/YiYuanYunGou 效果如下: 1:在主页面先定义按键跟绑定视图(没写全的都是属性中定义了比如btnRigth,dropdownView等):

Chosen中选择项的更新

Chosen 选择项的动态修改/更新 如果你需要去动态更新select选择框里的选择项,你需要通知Chosen去响应这个变动,你需要在这个选项框是触发一个"liszt:updated"事件,这样Chosen就会对更新过内容后的select选择框重新进行渲染. jQuery 版:$("#form_field").trigger("liszt:updated"); Prototype 版:Event.fire($("form_field&q

C# WinForm 选择目录路径和文件路径

private string SelectPath() //弹出一个选择目录的对话框 { FolderBrowserDialog path = new FolderBrowserDialog(); path.ShowDialog(); return path.SelectedPath; } private string SelectFile() //弹出一个选择文件的对话框 { OpenFileDialog file = new OpenFileDialog(); file.ShowDialog

CentOS6.9-zabbix3.2启动失败原因及页面没有mysql选择项

环境内核信息: [[email protected]01 ~]# uname -a Linux lodboyedu-01 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux zabbix启动失败的原因 启动时出这 个错误: [[email protected] zabbix-3.2.7]# /etc/init.d/zabbix_server start /usr/loca

把编译器的选择项设置为最严格状态

把编译器的选择项设置为最严格状态. 1 #include <iostream> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 6 7 8 using n

使ListView控件中的选择项高亮显示

实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewItemCollection SelectedItems{get;} 和ListViewItem数据项的BackColor属性 //获取或设置该数据项的背景色 public Color BackColor{get;set;} 实现代码: private void select_flash_Load(obj

手机端上下滑动选择项小组件

这是一个手机端的滑动选择小组件. 详细的需求介绍:话费充值,滑动选择充值面额,显示对应的应付金额即可. 重点请看Javascript部分的代码,请大神指点.跪谢!  贴代码~ CSS 部分: 1 html, body, h1, h2, h3, p, dl, dd, ol, ul, th, td, form, fieldset, input, button, textarea, a { 2 margin: 0; 3 padding: 0; } 4 5 html { 6 -webkit-text-s

C# winform 选择文件保存路径

winform 点击按钮选择文件保存的路径,效果如下图: 具体代码如下: private void button8_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { string foldPath =