• Windows Form -----内容(5)


    同一表格下的三级联动:

                   

    代码:

    class DBconnection
        {
            public const string CONNECTIONSTRING = "server=.;database=mydb;uid=sa;pwd=5587725";
        }

     class chinastates
        {
            public string AreaCode { get; set; }
            public string AreaName {get;set;}
            public string ParentAreaCode { get; set; }
        }

    class chianstatesDA
        {
            private SqlConnection _Conn;
            private SqlCommand _Cmd;
            private SqlDataReader _DR;
            public chianstatesDA()
            {
                _Conn = new SqlConnection(DBconnection.CONNECTIONSTRING);
                _Cmd = _Conn.CreateCommand();
            }
            public List<chinastates> Select(string parentareacode)
            {
                List<chinastates> list = new List<chinastates>();
                _Cmd.CommandText = "select * from chinastates where ParentAreaCode=@parentareacode";
                _Cmd.Parameters.Clear();
                _Cmd.Parameters.AddWithValue("@parentareacode",parentareacode);
                try
                {
                    _Conn.Open();
                    _DR = _Cmd.ExecuteReader();
                    while (_DR.Read())
                    {
                        chinastates data = new chinastates();
                        data.AreaCode = _DR["AreaCode"].ToString();
                        data.AreaName = _DR["AreaName"].ToString();
                        data.ParentAreaCode = _DR["ParentAreaCode"].ToString();
                        list.Add(data);
                    }
                }
                finally
                {
                    _Conn.Close();
                }
                return list;
            }

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Fillprov()
            {
                //取数据
                List<chinastates> list = new chianstatesDA().Select("0001");
                //填进去
                cmbProv.DataSource = list;
                cmbProv.DisplayMember = "AreaName";
                cmbProv.ValueMember = "AreaCode";
            }
            private void Fillcity()
            {
                string prt = (cmbProv.SelectedItem as chinastates).AreaCode;
                List<chinastates> list = new chianstatesDA().Select(prt);
                cmbCity.DataSource = list;
                cmbCity.DisplayMember = "AreaName";
                cmbCity.ValueMember = "AreaCode";
            }
            private void Fillcounty()
            {
                string prt = "";
                if (cmbCity.SelectedItem!=null)
                {
                    prt = (cmbCity.SelectedItem as chinastates).AreaCode;
                }
                List<chinastates> list = new chianstatesDA().Select(prt);
                cmbCounty.DataSource = list;
                cmbCounty.DisplayMember = "AreaName";
                cmbCounty.ValueMember = "AreaCode";
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                Fillprov();
                Fillcity();
                Fillcounty();
            }

            private void cmbProv_SelectedIndexChanged(object sender, EventArgs e)
            {
                Fillcity();
                Fillcounty();
            }

            private void cmbCity_SelectedIndexChanged(object sender, EventArgs e)
            {
                Fillcounty();
            }
        }

  • 相关阅读:
    VBE2019、VBE2014、VBE2014_VB6三合一的下载、安装和使用(最新版2020.09.09)
    调用其他VBA工程中的过程和函数以及API函数
    QQ消息群发助手SendQQMessage的下载和使用
    VBA/VB6/VBS/VB.NET/C#/Python/PowerShell都能调用的API封装库
    Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【C#版】
    Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】
    VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【C#版】
    VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】
    Outlook邮件的右键菜单中添加自定义按钮
    VBA自动点击IE的浏览按钮、自动选择路径、自动关闭打开文件对话框
  • 原文地址:https://www.cnblogs.com/likaixuan/p/4503360.html
Copyright © 2020-2023  润新知