欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#用ComboBox控件实现省与市的联动效果的方法

程序员文章站 2023-12-12 16:55:34
本文实例讲述了c#用combobox控件实现省与市的联动效果的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:using system; ...

本文实例讲述了c#用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; 
using system.configuration; 
 
namespace 省市联动 

    public partial class form1 : form 
    { 
        public form1() 
        { 
            initializecomponent(); 
        } 
 
        private void form1_load(object sender, eventargs e) 
        { 
            tsql.getconnection(); 
            using (sqlconnection conn = new sqlconnection(tsql.getconnection())) 
            { 
                conn.open(); 
                using (sqlcommand cmd = conn.createcommand()) 
                { 
                    cmd.commandtext = "select * from promary"; 
                    using (sqldatareader dr = cmd.executereader()) 
                    { 
                        while (dr.read()) 
                        { 
                            province pr = new province(); 
                            pr.proid = dr.getint32(dr.getordinal("proid"));  //这里要特别注意数据库字段的类型 proid在数据库里是int型 
                            pr.proname = dr.getstring(dr.getordinal("proname")); 
                            combobox1.items.add(pr);//pr是一个province对象。province有一个proname和proid属性。我将一个对象填充到combobox1里。再让combobox1的displaymember属性设置proname。【也就是让combobox1显示pr对象的proname属性】。这样在填充市级城市的时候我就可以找到当前选择项combobox1.selecteditem(或者当前选择对象)所对应的proid 
                        } 
                    } 
                } 
                combobox1.selectedindex = 0;  //将comcombobox1的默认选项设为0 
            } 
        } 
 
        private void combobox2_selectedindexchanged(object sender, eventargs e) 
        { 
        } 
 
        private void combobox1_selectedindexchanged(object sender, eventargs e) 
        { 
            combobox2.items.clear(); //在选择的时候先清空一下combox1里面的所以项,以免在更换省名的时候,与先前对应的省名下的市没有删除。 
            province tem = (province)combobox1.selecteditem; // 因为在combobox1.items.add(pr)里添加的是对象。所以在这里讲combox1所选中的当前对象赋给tem对象 (tem对象下面有两个属性 proname和proid) 
             
            int id = tem.proid;  //将当前选中对象的的proid赋值给id,给下面的sql查询语句使用 
            tsql.getconnection();// 获取数据库连接字符串。 
            using (sqlconnection conn = new sqlconnection(tsql.getconnection())) 
            { 
                conn.open(); 
                using (sqlcommand cmd = conn.createcommand()) 
                { 
                     
                    cmd.commandtext = "select * from city where proid=@id"; 
                    cmd.parameters.add(new sqlparameter("id", id)); 
                    using (sqldatareader dr = cmd.executereader()) 
                    { 
                        while (dr.read()) 
                        { 
                            combobox2.items.add(dr.getstring(dr.getordinal("cityname"))); 
                        } 
                    } 
                } 
                combobox2.selectedindex = 0; //将comcombobox1的默认选项设为0 
            } 
        } 
    } 
    class tsql 
    { 
        public static string getconnection() //数据库连接字符串 
        { 
            string getconn = configurationmanager.connectionstrings["getconn"].connectionstring; 
            return getconn; 
        }        
    } 
    class province 
    { 
        public string proname { get; set; } 
        public int proid { get; set; } 
    } 
}

复制代码 代码如下:
using system.data.sqlclient;
namespace 省市选择2
{
    public partial class form1 : form
    {
        publicform1()
        {
            initializecomponent();
        }
        privatevoid form1_load(objectsender,eventargs e)
        {
            /*
            //测试代码:执行这段代码的情况下是可以吧 tom这个值添加到 combobox1中的。
            people p1 = new people();
            p1.name = "tom";
            p1.age = 25;
            combobox1.items.add(p1.name);
            */
            using(sqlconnection conn =newsqlconnection("data source=凡斌-vaio;initialcatalog=dbpromary;integrated security=true"))
            {
                conn.open();
                using(sqlcommand cmd = conn.createcommand())
                {
                    cmd.commandtext = "select * from promary";
                    using(sqldatareader reader = cmd.executereader())
                    {
                        while (reader.read())
                        {
                            //string str =reader.getstring(reader.getordinal("proname"));
                            //combobox1.items.add(str);
                            province  pv =new  province();
                            //将combobox1控件的displaymember属性设置为“provincename”用来在combobox1控件中显示provincename属性的值
                           combobox1.displaymember = "provincename";
                            pv.provincename =reader.getstring(reader.getordinal("proname"));
                            pv.provinceid =reader.getint32(reader.getordinal("proid"));
                            //虽然pv属于对象,把一个对象添加到comboxbox1中,控件显示的值为对象的类名,但是在前面combobox1.displaymember ="provincename"处已经设置好控件要显示的为 province类的provincename值,所以添加进去的是pv对象的provincename值
                            combobox1.items.add(pv);
                        }                     
                    }
                }
            }
        }
 
        privatevoid combobox1_selectedindexchanged(object sender,eventargse)       //注意这里是combobox1不是combobox2
        {
            try
            {
                province  tem = (province) combobox1.selecteditem;
                int id = tem.provinceid;
 
                combobox2.items.clear();
                using(sqlconnection conn =newsqlconnection("data source=凡斌-vaio;initialcatalog=dbpromary;integrated security=true"))
                {
                    conn.open();
                    using (sqlcommand cmd = conn.createcommand())
                    {
                        //查询city(市)表里的 proid与promary(省)表里proid相同的所有数据。
                        cmd.commandtext = "select * from city where proid =@id";
                        cmd.parameters.add(new sqlparameter("id", id));
                        sqldatareader dr = cmd.executereader();
 
                        while(dr.read())
                        {
                           combobox2.items.add(dr.getstring(dr.getordinal("cityname")));
                        }
                    }
                }
            }
            catch(exception ex)
            {
                messagebox.show("错误信息:" + ex.message);
            }
        }
    }
  
    class province
    {
        public string provincename { get;set; }
        public int provinceid { get;set; }
    }
    //class people
    //{
    //   public string name { get; set; }
    //   public int age { get; set; }
    //}
}

如下图所示:

C#用ComboBox控件实现省与市的联动效果的方法

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: