private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { List <string> city = new List <string>(); TblArea tblArea = this.comboBox1.SelectedItem as TblArea; if (tblArea != null) { string sql = "SELECT AreaId,AreaName FROM TblArea WHERE AreaPId=@AreaPId"; SqlParameter parameters = new SqlParameter("@AreaPId", SqlDbType.Int) { Value = tblArea.AreaId }; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, parameters)) { if (reader.HasRows) { while (reader.Read()) { city.Add(reader.GetString(1)); } } } //数据绑定 this.comboBox2.DataSource = city; } }
private void Form1_Load(object sender, EventArgs e) { //using (SqlConnection sqlConn = new SqlConnection(conStr)) //{ List <TblArea> listArea = new List <TblArea>(); //先获取根节点 string sql = string.Format("SELECT AreaId,AreaName FROM TblArea WHERE AreaPId=@AreaPId"); //绑定参数 SqlParameter parameters = new SqlParameter() { ParameterName = "@AreaPId", Value = 0 }; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, parameters)) { if (reader.HasRows) { while (reader.Read()) { TblArea tblArea = new TblArea(); tblArea.AreaId = reader.GetInt32(0); tblArea.AreaName = reader.GetString(1); listArea.Add(tblArea); } } } //向treeview中添加节点 foreach (TblArea item in listArea) { TreeNode node = this.treeView1.Nodes.Add(item.AreaName); node.Tag = item.AreaId; LoadTreeView(item.AreaId, node.Nodes); } //using (SqlCommand sqlComm = new SqlCommand(sql, sqlConn)) //{ // sqlConn.Open(); // using (SqlDataReader reader = sqlComm.ExecuteReader()) // { // if (reader.HasRows) // { // while (reader.Read()) // { // TreeNode node = this.treeView1.Nodes.Add(reader.GetString(1)); // LoadTreeView(reader.GetInt32(0), node.Nodes); // } // } // } //} //} }
private void Form3_Load(object sender, EventArgs e) { List <TblArea> listArea = new List <TblArea>(); //Dictionary<int, string> listArea = new Dictionary<int, string>(); string sql = "SELECT AreaId,AreaName FROM TblArea WHERE AreaPId=@AreaPId"; SqlParameter parameters = new SqlParameter() { ParameterName = "@AreaPId", Value = 0 }; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, parameters)) { if (reader.HasRows) { while (reader.Read()) { TblArea area = new TblArea(); area.AreaId = reader.GetInt32(0); area.AreaName = reader.GetString(1); listArea.Add(area); //listArea.Add(reader.GetInt32(0), reader.GetString(1)); } } } //数据绑定 this.comboBox1.DisplayMember = "AreaIName"; this.comboBox2.ValueMember = "AreaId"; this.comboBox1.DataSource = listArea; //foreach (KeyValuePair<int,string> item in listArea) //{ // this.comboBox1.Items.Add(item.Value); //} }