zoukankan      html  css  js  c++  java
  • 自建List<>绑定ComboBox下拉框实现省市联动

     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()方法
    
                            }
                        }
                    }
                }
               
            }
        }
    }
  • 相关阅读:
    NET領域模型(1)
    oracle 函數索引(1)
    oracle 動態SQL(1)
    WF事件驱动(5)
    WF事件驱动(3)
    WF事件驱动(4)
    HP LoadRunner 11.00 新鲜尝
    Tomcat配置优化要点
    WebSphere性能诊断与调优
    性能测试工具、监控工具
  • 原文地址:https://www.cnblogs.com/blacop/p/6057682.html
Copyright © 2011-2022 走看看