首页 > 代码库 > 2017-5-7 三级联动

2017-5-7 三级联动

技术分享

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 三级联动.App_code;

namespace 三级联动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            bind("0001",comboBox1);
            bind(comboBox1.SelectedValue.ToString(),comboBox2);
            bind(comboBox2.SelectedValue.ToString(),comboBox3);
            
        }
        public void bind(string pcode, ComboBox cb)
        {

            List<chinastate> clist = new chinastatedata().select(pcode);
            cb.DataSource = clist;
            cb.DisplayMember = "AreaName";
            cb.ValueMember = "AreaCode";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind(comboBox1.SelectedValue.ToString(), comboBox2);
            if (comboBox2.SelectedValue!=null)
            {
            bind(comboBox2.SelectedValue.ToString(), comboBox3);
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind(comboBox2.SelectedValue.ToString(), comboBox3);
        }
    }
}

技术分享

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 三级联动.App_code
{
   public  class chinastate
    {
        private string _AreaCode;

        public string AreaCode
        {
            get { return _AreaCode; }
            set { _AreaCode = value; }
        }
        private string _AreaName;

        public string AreaName
        {
            get { return _AreaName; }
            set { _AreaName = value; }
        }
        private string _ParentAreaCode;

        public string ParentAreaCode
        {
            get { return _ParentAreaCode; }
            set { _ParentAreaCode = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 三级联动.App_code
{
    public class chinastatedata
    { 
        SqlConnection conn=null;
        SqlCommand cmd=null;

        public chinastatedata()
     {
         conn = new SqlConnection("server=.;database=data0216;user=sa;pwd=123");
         cmd = conn.CreateCommand();
     }

        public List<chinastate> select(string pcode) 
        {
            List<chinastate> clist = new List<chinastate>();
            cmd.CommandText = "select * from ChinaStates where ParentAreaCode=@a";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@a",pcode);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                chinastate c = new chinastate();
                c.AreaCode = dr[0].ToString();
                c.AreaName = dr[1].ToString();
                c.ParentAreaCode = dr[2].ToString();
                clist.Add(c);

            }
            conn.Close();

            return clist;
        }




    }
    
}

 

2017-5-7 三级联动