2012-12-31 51 views
0

我是一名學生,和一個新手編程,我有兩個組合框,combobox1和combobox2 combobox1包含移動公司像諾基亞,三星,HTC和combobox2包含像三星,S3等移動模型等,我想排序兩個組合框我的意思是,當我在combobox1點擊諾基亞那麼諾基亞的所有機型應該是combobo2列表中可見,所以我決定使用外鍵關係填充ComboBox2取決於Combobox1選擇項目

  Manufacturer -table 
     - manufacturerid (primary key) 
     - name 

     Model -table 
     - modelid (primary key) 
     - manufacturerid (foreign key to manufacturer) 
     - name 

例數據:

製造商表

  manufacturerid name 
    -------------- ---------- 
    1    Nokia 
    2    Samsung 
    3    HTC 

型號表

 modelid manufacturerid name 
    ------- -------------- ---------- 
    1  1    C7 
    2  1    Lumia 900 
    3  1    Lumia 920 
    4  2    Galaxy S II 
    5  2    Galaxy S III 
    6  3    Desire X 
    7  3    Windows Phone 8X 
    8  3    One S 

我想,如果我在第一個組合框中選擇諾基亞則第二個下拉框將選擇其manufactureid = 1 使用什麼型號的所有?我怎樣才能做到這一點? priviously我使用

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    comboBox3.Text = ""; 
    if ("samsung" == comboBox4.SelectedItem.ToString()) 
     { 
      comboBox3.DataSource = table1BindingSource; 
      comboBox3.ValueMember = "samsung"; 
      comboBox3.DisplayMember = "samsung"; 
     } 
    if ("htc" == comboBox4.SelectedItem.ToString()) 
     { 
     comboBox3.DataSource = table1BindingSource; 
     comboBox3.ValueMember = "htc"; 
      comboBox3.DisplayMember = "htc"; 
     } 
    } 

,但我有我每次添加一個新的模型來更新三星字符串,所以我決定用表格的工作,這樣我可以更新

+2

你試過沒呢? –

+0

上面的例子我用一列公司的整個列,但有一些問題,所以我dec dec ided與這個外鍵工作 –

+0

請看我的回答 –

回答

2

1日組合框SelectedIndexChanged事件(製造組合框) 取選擇manufacturerid 再火這個查詢來填充其他組合框,即(模型)

Select modelid,name from modeltable where [email protected]

背後的代碼 東西就像這樣,我只寫了thi沒有IDE S碼看看,可能需要做一些修改

private void monufactureComobobox_SelectedIndexChanged(object sender, EventArgs e) 
{ 

string fecthManufacturerID= manufactureComobobox.selectedItem; 
DataTable dtModel = new DataTable(); 
dtModel= ModelComboPopulate(fecthManufacturerID); 
ModelcomboBox.DataSource = dtModel; 
ModelcomboBox.ValueMember = "modelid"; 
ModelcomboBox.DisplayMember = "name"; 

} 

public DataTable ModelComboPopulate(string ID) 
{ 
DataSet ds = new DataSet(); 
using (SqlConnection con = new SqlConnection(connection)) 
{ 
    string myquery="Select modelid,name from modeltable where [email protected]"; 
    SqlCommand cmd = new SqlCommand(myquery, con); 
    SqlDataAdapter dap = new SqlDataAdapter(); 
    dap.SelectCommand = cmd; 
    cmd.Parameters.Add("@combox1Value", SqlDbType.NVarChar, 15).Value = ID; 
    dap.Fill(ds); 
    return ds.Tables[0]; 
    } 

} 
+0

paaji,我必須直接寫這個嗎? private void comboBox4_SelectedIndexChanged(object sender,EventArgs e) {Select modelid,modeltable中的名稱其中manufactuerid = @ combox1Value } –

+0

@shariq_khan:Bhai,您希望填充組合框還是要更新數據? –

+0

paaji我想填充組合框。當選擇combobox1諾基亞時,製造商ID爲1的所有諾基亞型號都將安裝在combobox2中。像這樣 –

2

包含一個函數來檢索如下

public DataTable Select(String sqlQuery) 
    {  
     con.Open(); 
     SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery,con); 
     DataTable table = new DataTable(); 
     adapter.Fill(table); 
     con.Close(); 
     return table; 
    } 

Page_Load事件從數據庫數據

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!Page.IsPostBack) 
    { 
     String sqlQuery="select manufacturerid,name From Manufacturertable"; 

     comboBox4.DataSource = cls.Select(sqlQuery); 
     comboBox4.DataTextField = "name"; 
     comboBox4.DataValueField = "manufacturerid"; 
     comboBox4.DataBind(); 
    } 
} 

SelectedIndexChanged事件comboBox4

protected void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { 
    String sqlQuery="select modelid,name From Modeltable where manufacturerid="+ Convert.ToInt16(comboBox4.SelectedValue.ToString()); 

    comboBox3.DataSource = cls.Select(sqlQuery); 
    comboBox3.DataTextField = "name"; 
    comboBox3.DataValueField = "modelid"; 
    comboBox3.DataBind(); 
} 
+0

http://stackoverflow.com/questions/14162839/how-to - 將文本從動態生成用戶控制文本框轉換爲文本框 你能幫我嗎BHAI請用 –

2

使用DataSetDataTable存儲兩個表,然後在列表中字符串strArr填充字符串。喜歡的東西如下:(至少應邏輯應該工作

List<string> strArr = new List<string>(); 
strArr.Items.Clear(); 
for(int intSubCount = 0; intSubCount < dtTable2.Rows.Count;intSubCount++) 
{ 
    if(MyComboBox.Text.Equals(dtTable2.Rows[intSubCount]["modelid"].ToString())) 
    { 
     strArr.Add(dtTable2.Rows[intSubCount]["name"].ToString()); 
    } 
} 
// 
comboBox3.DataSource = strArr; 

否則

最簡單的方法是使用DataView

DataView dv = dtTable1.defaultView; 
    dv.RowFilter("modelid = '" + myComboBox.Text + "'"); 
    //use DataView to populate the Second ComboBox. 
+0

http://stackoverflow.com/questions/14162839/how-to-transfer-the-text-from-dynamically-generated-user-control-to-a-textbox 你能幫助我嗎? –