none
有關於comboBox1_SelectedIndexChanged事件 RRS feed

  • 問題

  • 問題是這樣的:

    如何讓comboBox1所選取的縣市對應到comboBox2所對應的鄉鎮市?

    EX:

    comboBox1選擇高雄市時,comboBox2就會列出高雄市所包含的鄉鎮市(左營區、楠梓區、小港區...等)?

    資料表名稱Zip32    (表內容:郵局網站3+2郵遞區號excel檔)

    欄位名稱:Counrty(縣市)、City(鄉鎮市)、Road(路)

    程式碼如下:

    private void Form1_Load(object sender, EventArgs e)
            {
                //*****(combobox1擷取DB資料--縣市)*****

                SqlConnection Conn = new SqlConnection(strSelect);  //DB連線
                Conn.Open();

                //擷取資料欄(Country

                String SelectCountry = " Select distinct(Country) From Zip32 ";
                SqlDataAdapter adapter1 = new SqlDataAdapter(SelectCountry, Conn);
                DataSet mycountry = new DataSet();
                adapter1.Fill(mycountry, "Country");
               
                //將DataSet的資料填入combobox1
                comboBox1.DataSource = mycountry.Tables["Country"];
                comboBox1.DisplayMember = "Country";


                //*****(combobox2--鄉鎮市)****

                String SelectCity = " Select distinct(City) From Zip32 ";
                SqlDataAdapter adapter2 = new SqlDataAdapter( SelectCity, Conn );
                DataSet mycity = new DataSet();
                adapter2.Fill(mycity,"City");

                comboBox2.DataSource = mycity.Tables["City"];
                comboBox2.DisplayMember = "City";


            }

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
               
                SqlConnection Conn = new SqlConnection(strSelect); 
                Conn.Open();

    Sqlstr=”select * from Zip32 Where ”Zip32.Country=comboBox1.selectedvalue;

     

    For(int i=0;i<rows.count-1;i++))

    {

      comboBox2.items.add(“”);

      comboBox2.itemsIdea.text=”內容編號”;

      comboBox2.itemsIdea.value=”內容名稱”;

    }

     

    在comboBox1_SelectedIndexChanged中的程式好像不太對....

    請問各位大大,是否有比較正確的寫法??

    還請不吝指導!!

    語言C#-(WinFrm)
    DB:MSSQL2005
    工具:VS2005

     

     

     

     

    2007年3月8日 下午 02:15

解答

所有回覆