首页 > 代码库 > ComboBox,三级联动菜单,新入门点小白,有些代码有待优化,大神勿喷
ComboBox,三级联动菜单,新入门点小白,有些代码有待优化,大神勿喷
//当前窗体的Load事件 private void provinceANDCity_Load(object sender, EventArgs e) { //连接字符串 string strConn = "Data Source=.;Initial Catalog=Province;User Id=sa;Pwd=ok"; //connection对象 SqlConnection conn = new SqlConnection(strConn); //DataSet对象 DataSet ds = new DataSet(); //查询省份语句 string sql = "SELECT * FROM province"; SqlDataAdapter adapter = new SqlDataAdapter(sql,conn); adapter.Fill(ds,"province"); //将省份的数据绑定到省份的下拉框中 this.cboProvince.DataSource = ds.Tables["province"]; this.cboProvince.ValueMember = "id"; this.cboProvince.DisplayMember = "provinceName"; //获取省份编号 int id = Convert.ToInt32(this.cboProvince.SelectedValue); //根据省份编号查询城市 sql = "SELECT * FROM City WHERE proId="+id; adapter = new SqlDataAdapter(sql,conn); adapter.Fill(ds,"City"); //将查出来的城市绑定在城市下拉框中 this.cboCity.DataSource = ds.Tables["City"]; this.cboCity.ValueMember = "id"; this.cboCity.DisplayMember = "cityName"; //获取城市编号 int cityId = Convert.ToInt32(this.cboCity.SelectedValue); //根据城市编号查询区县 sql = "SELECT * FROM County WHERE cityId=" + cityId; adapter = new SqlDataAdapter(sql, conn); adapter.Fill(ds, "County"); //将查出来的区县绑定在区县下拉框中 this.cboCounty.DataSource = ds.Tables["County"]; this.cboCounty.ValueMember = "id"; this.cboCounty.DisplayMember = "countyName"; } //省份下拉框的事件 private void cboProvince_SelectionChangeCommitted(object sender, EventArgs e) { string strConn = "Data Source=.;Initial Catalog=Province;User Id=sa;Pwd=ok"; SqlConnection conn = new SqlConnection(strConn); DataSet ds = new DataSet(); //获取省份编号 int id = Convert.ToInt32(this.cboProvince.SelectedValue); //根据省份编号查询城市 string sql = "SELECT * FROM City WHERE proId="+id; SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); adapter.Fill(ds, "City"); //将查出来的城市绑定在城市下拉框中 this.cboCity.DataSource = ds.Tables["City"]; this.cboCity.ValueMember = "id"; this.cboCity.DisplayMember = "cityName"; } //城市下拉框的事件 private void cboCity_SelectionChangeCommitted(object sender, EventArgs e) { string strConn = "Data Source=.;Initial Catalog=Province;User Id=sa;Pwd=ok"; SqlConnection conn = new SqlConnection(strConn); DataSet ds = new DataSet(); //获取城市编号 int cityId = Convert.ToInt32(this.cboCity.SelectedValue); //根据城市编号查询区县 string sql = "SELECT * FROM County WHERE cityId=" + cityId; SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); adapter.Fill(ds, "County"); //将查出来的区县绑定在区县下拉框中 this.cboCounty.DataSource = ds.Tables["County"]; this.cboCounty.ValueMember = "id"; this.cboCounty.DisplayMember = "countyName"; }
ComboBox,三级联动菜单,新入门点小白,有些代码有待优化,大神勿喷
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。