SoFunction
Updated on 2025-03-07

Method for using ComboBox control to achieve the linkage effect between province and city

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
 
namespace provincial and municipal linkage

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            (); 
            using (SqlConnection conn = new SqlConnection(())) 
            { 
                (); 
                using (SqlCommand cmd = ()) 
                { 
                    = "select * from promary"; 
                    using (SqlDataReader dr = ()) 
                    { 
                        while (()) 
                        { 
                            Province pr = new Province(); 
= dr.GetInt32(("proID")); // Here you should pay special attention to the type of the database field proID is int type in the database.
                            = (("proName")); 
(pr);//pr is a Province object. Province has a proName and proID attributes. I populate an object into comboBox1. Then set the displayMember property of comboBox1 to set the proName. [That is, let comboBox1 display the proName property of the pr object]. In this way, when filling in a municipal city, I can find the proID corresponding to the current selection (or the current selection object).
                        } 
                    } 
                } 
= 0; //Set the default option of comcomboBox1 to 0
            } 
        } 
 
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
        { 
        } 
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
(); //When selecting, clear the so-called items in combox1 first, so as not to delete the cities under the previous corresponding province name when changing the provincial name.
Province tem = (Province); // Because what is added in (pr) is an object. So here we say that the current object selected by comBox1 is assigned to the tem object (there are two attributes proName and proID under the tem object)
             
int id = ;  // Assign the proID of the currently selected object to id and use it for the following SQL query statement
();// Get the database connection string.
            using (SqlConnection conn = new SqlConnection(())) 
            { 
                (); 
                using (SqlCommand cmd = ()) 
                { 
                     
                    = "select * from city where proID=@id"; 
                    (new SqlParameter("id", id)); 
                    using (SqlDataReader dr = ()) 
                    { 
                        while (()) 
                        { 
                            ((("cityName"))); 
                        } 
                    } 
                } 
= 0; //Set the default option of comcomboBox1 to 0
            } 
        } 
    } 
    class Tsql 
    { 
public static string GetConnection() //Database connection string
        { 
            string getConn = ["getConn"].ConnectionString; 
            return getConn; 
        }        
    } 
    class Province 
    { 
        public string proName { get; set; } 
        public int proID { get; set; } 
    } 
}