Visual Studio DropDownList没有下降

时间:2017-04-12 03:57:25

标签: c# winforms visual-studio

我正在使用Visual Studio中的c#表单。我有两个组合框,第一个选择国家,第二个选择区域/州/等

如果我将数据提供给列表框似乎工作正常,所以我知道数据存在,但是当我尝试将其放入combobox2时我无法将其下载

任何帮助都会非常感激,因为我真的被困了

SqlConnection conn = new SqlConnection("Data Source=ITS40720;Initial Catalog=VisualStudio;Integrated Security=True;");
conn.Open();
SqlCommand sc = new SqlCommand("select SubdivisionName from dbo.Locations where CountryName = '" + comboBox1.Text + "'", conn);
SqlDataReader reader;

reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("Countryname", typeof(string));
dt.Columns.Add("SubdivisionName", typeof(string));
dt.Load(reader);

comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
comboBox2.FormattingEnabled = true;
comboBox2.ValueMember = "Countryname";
comboBox2.DisplayMember = "SubdivisionName";
comboBox2.DataSource = dt;

conn.Close();

3 个答案:

答案 0 :(得分:0)

您的SQL查询应该是

select SubdivisionName, Countryname from ...

并检查查询结果,也许它不返回任何数据。

不要将Countryname用作combobox2的ValueMember,我建议使用SubdivisionId intead。

select SubdivisionName, SubdivisionId from ...

答案 1 :(得分:0)

根据您的查询,它不会返回与组合框相关的任何值,查询应选择 SubdivisionName, Countryname

SqlCommand sc = new SqlCommand("select SubdivisionName,Countryname from dbo.Locations where CountryName = '" + comboBox1.Text + "'", conn);

comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
comboBox2.FormattingEnabled = true;
comboBox2.ValueMember = "Countryname";
comboBox2.DisplayMember = "Countryname";
comboBox2.DataSource = dt;

答案 2 :(得分:0)

您没有获得两个必需的下拉列

您的SQL查询应该是

.nodecor:hover, .nodecor:active.nodecor:focus { text-decoration: none;}