2017-05-13 72 views
0

我有下面的C#代碼可以工作,所以當我選擇國家下拉菜單時,會填充狀態下拉列表。級聯下拉控件

如果選擇狀態下拉菜單(comboBox2),我想要做的是將相應的國家(comboBox1)selectedIndex更改爲相應的國家/地區。

例如:選擇古吉拉特時,應在第一comboBox1顯示印度

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 cascadingdropdownlist 
{ 
    public partial class Form1 : Form 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"); 
     DataRow dr; 

     public Form1() 
     { 
      InitializeComponent(); 
      refreshdata(); 
     } 

     public void refreshdata() 
     { 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("select * from tbl_country", con); 
      SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      DataTable dt = new DataTable(); 
      sda.Fill(dt); 
      con.Close(); 
      dr = dt.NewRow(); 
      dr.ItemArray = new object[] { 0, "--Select Country--" }; 
      dt.Rows.InsertAt(dr, 0); 
      comboBox1.ValueMember = "countryid"; 
      comboBox1.DisplayMember = "countryname"; 
      comboBox1.DataSource = dt; 
     } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (comboBox1.SelectedValue.ToString()!= null) 
      { 
       int countryid = Convert.ToInt32(comboBox1.SelectedValue.ToString()); 
       refreshstate(countryid); 
      } 
     } 

     public void refreshstate(int countryid) 
     { 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("select * from tbl_state where countryid= @countryid", con); 
      cmd.Parameters.AddWithValue("countryid", countryid); 
      SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      DataTable dt = new DataTable(); 
      sda.Fill(dt); 
      con.Close(); 
      dr = dt.NewRow(); 
      dr.ItemArray = new object[] { 0, "--Select State--" }; 
      dt.Rows.InsertAt(dr, 0); 

      comboBox2.ValueMember = "stateid"; 
      comboBox2.DisplayMember = "statename"; 
      comboBox2.DataSource = dt; 
     } 
    } 
} 

屏幕 enter image description hereenter image description hereenter image description here

+1

沒有國家的,國家不會根據你的代碼填充?那麼你如何選擇狀態,而不是選定的國家? – Krishna

+0

@Krishna默認情況下,表單加載時的兩個下拉列表應顯示所有狀態和所有國家 – user580950

+0

我沒有在表單加載中看到它,我只看到國家/地區數據加載,無論如何,您面臨的問題是什麼? – Krishna

回答

0

將有兩個途徑級聯對象。考慮到您在開始時已經加載了組合框,您應該處理combobox2選擇更改事件。您可以將SelectedValue轉換爲適當的對象,而不是處理值,以便您可以提取countryid值。然後,您應該設置combobox1的selectedValue。你應該注意,如果你不阻止comboBox1_SelectedIndexChanged的執行,會有一個惡性循環。因此,我使用了doNotCascade = true;強調這個問題,你應該檢查comboBox1_SelectedIndexChanged事件處理程序中的這個變量。

希望有所幫助。

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DataRowView item = comboBox2.SelectedItem as DataRowView; 
    if (item != null && this.comboBox2.SelectedIndex != 0) 
    { 
     doNotCascade = true; 
     this.comboBox1.SelectedValue = item.Row[2]; 
    } 
} 
0

首先,你需要編寫這個sub query

create proc sp_FindCountryName 
@stateName varchar(40) 
as 
begin 
    select countryName from country where countryID=(
    select countryID from [State] where [email protected]) 
end 

然後實現comboBox2_SelectedIndexChanged方法(這裏cbState是第二comboBox

private void cbState_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (cbState.SelectedValue.ToString() != null) 
    { 
     string stateName = this.cbState.GetItemText(this.cbState.SelectedItem); 
     retriveCountryName(stateName); 
    } 

} 

private void retriveCountryName(string stateName) 
{ 
    using (con = new SqlConnection(connectionString)) 
    { 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("sp_FindCountryName", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@stateName", stateName); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     dr = dt.NewRow(); 
     cbCountry.ValueMember = "countryID"; 
     cbCountry.DisplayMember = "countryName"; 
     cbCountry.DataSource = dt; 

    } 
}