C#三级联动

1.运用ComboBox的控件建立效果,如右图:

2.建立三个表,第一个Province表: PID,PName;第二个为city表: CId,CName,PId;第三个为coun表:CounID,CounName,CId;

3.

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

namespace 三级联动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
  
        }
        private void BindProvince()
        {
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=三级联动;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("select PID, PName from prov", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);

            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    this.comboBox1.Items.Add(dt.Rows[i]["PName"].ToString());
            //}

            this.comboBox1.DataSource = dt;
            this.comboBox1.DisplayMember = "PName";
           

        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)comboBox1.SelectedItem;
                string pId = drv.Row["PID"].ToString();
                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=三级联动;Integrated Security=True");
                SqlDataAdapter da = new SqlDataAdapter("select CId, CName from City where PId='" + pId + "'", conn);
                DataSet ds = new DataSet();
                da.Fill(ds,"City");
                DataTable dtUser = ds.Tables["City"];
                comboBox2.DataSource = dtUser;
                comboBox2.DisplayMember = "CName";
                comboBox2.ValueMember = "CID";
             
            }
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            BindProvince();  //调用绑定的方法
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)comboBox2.SelectedItem;
                string cId = drv.Row["CId"].ToString();
                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=三级联动;Integrated Security=True");
                SqlDataAdapter da = new SqlDataAdapter("select CounID,CounName from coun where CId='" + cId + "'", conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Coun");
                DataTable dtUser = ds.Tables["Coun"];
                comboBox3.DataSource = dtUser;
                comboBox3.DisplayMember = "CounName";
                comboBox3.ValueMember = "CounID";
            }
        }
    }
}

 

posted @ 2016-01-20 17:17  _Nestling  阅读(2026)  评论(0编辑  收藏  举报