标签:选中 int change nbsp cti 获取 联动 string bsh
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 _04省市联动 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //所有的省份显示到第一个下拉框中 LoadAreaByAreaPid(0);//参数是列名AreaId的值 } private void LoadAreaByAreaPid(int p)//参数p是列名AreaId的值 { List<Area> list = new List<Area>(); list.Add(new Area() { AreaId = -1, AreaName = "请选择" }); string sql = "select AreaId,AreaName from TblArea where AreaPId=" + p;//参数p是列名AreaId的值 using (SqlDataReader reader = SqlHelper.ExecuteReader(sql)) { if (reader.HasRows) { while (reader.Read()) { Area a = new Area(); a.AreaId = Convert.ToInt32(reader["AreaId"]); a.AreaName = reader["AreaName"].ToString(); list.Add(a); } } } cmbSheng.DataSource = list;//绑定数据源 cmbSheng.DisplayMember = "AreaName";//主要是设置下拉框显示的值 cmbSheng.ValueMember = "AreaId";//实际值 } private void cmbSheng_SelectedIndexChanged(object sender, EventArgs e) { if (cmbSheng.SelectedIndex!=0) { cmbCheng.Items.Clear();//清空 //获取当前选中的省份的id int id = Convert.ToInt32(cmbSheng.SelectedValue); string sql = "select AreaId,AreaName from TblArea where AreaPId=" + id; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql)) { if (reader.HasRows) { while (reader.Read())//练习 { Area a = new Area(); a.AreaId = Convert.ToInt32(reader["AreaId"]); a.AreaName = reader["AreaName"].ToString(); cmbCheng.Items.Add(a);//自带集合,要重写ToString()方法 } } } } } } }
标签:选中 int change nbsp cti 获取 联动 string bsh
原文地址:http://www.cnblogs.com/blacop/p/6057682.html