2017-09-04 38 views
0

當我改變DropDownList的ddlCO的價值,我不能清潔值的DropDownListddlCountry。我使用DataSet清除的DropDownList值C#

上的值DropDownListddlCountry合併。

如何解決這個問題?

我的代碼如下,預先感謝您的任何幫助,真的很感激。

protected void ddlCO_SelectedIndexChanged(object sender, EventArgs e) 
{ 

    sql = @String.Format(" SELECT * FROM "); 
    sql += String.Format(" `tbl_1` WHERE Country IN (?); "); 

    using (OdbcConnection cn = 
     new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) 
    { 
     using (OdbcCommand cmd = 
      new OdbcCommand(sql, cn)) 
     { 
      cmd.Parameters.AddWithValue("param1", ddlCO.SelectedItem.Value); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection.Open(); 

      using (OdbcDataAdapter sda = 
       new OdbcDataAdapter(cmd)) 
      { 
       ddlCountry.AppendDataBoundItems = true; 
       ddlCountry.ClearSelection(); 
       ddlCountry.Enabled = true; 

       DataSet ds = new DataSet(); 
       sda.Fill(ds); 
       ddlCountry.DataSource = ds.Tables[0]; 
       ddlCountry.DataTextField = "CountryText"; 
       ddlCountry.DataValueField = "CountryValue"; 
       ddlCountry.DataBind(); 
      } 
     } 
    } 
} 
+0

嘗試以清除它'.DataSource = null' – EpicKip

+0

@EpicKip謝謝你的幫助,但* DropDownList的* ** ** ddlCountry不空。 –

+0

該列表是否仍然填滿或僅包含上次選擇的項目的文本?因爲如果我將數據源設置爲空,我的組合框立即清除 – EpicKip

回答

0

您可以通過多種方式清除組合框,如果它有一個DataSource你它的數據源設置爲null

myComboBox.DataSource = null; 

,如果它不是一個數據源,你可以使用items集合中的組合框:

myComboBox.Items.Clear();