C# Visual Studio下拉列表未下拉

C# Visual Studio下拉列表未下拉,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我正在Visual Studio中编写c窗体。我有两个组合框,第一个选择国家,第二个选择地区/州/等 如果我将数据输入列表框,它似乎工作正常,因此我知道数据在那里,但当我尝试将其放入combobox2时,我无法将其下拉 非常感谢您的帮助,因为我真的被卡住了 SqlConnection conn = new SqlConnection("Data Source=ITS40720;Initial Catalog=VisualStudio;Integrated Security=True;"); co

我正在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();

您的sql查询应该是

select SubdivisionName, Countryname from ...
并检查查询结果,可能它不会返回任何数据

不要将Countryname用作Combox2的ValueMember,我建议使用SubjectionID intead of

select SubdivisionName, SubdivisionId from ...

根据您的查询,它不会返回任何与combobox相关的值,查询应选择SubsectionName、Countryname


您没有获得下拉列表所需的两列

您的SQL查询应该是

select SubdivisionName, Countryname from ...

选择CountryName,SubsectionName from dbo。CountryName='COUNTRY'

查询本身工作正常的位置,我知道这是因为数据显示在列表框中没有问题。当我尝试将该数据放入下拉列表时,单击不会使其下拉。我已经删除了CountryName的列,但它是否返回了相关数据?它确实返回了我希望它返回的内容。如果我添加了第三个样式简单的组合框,它会显示出来,并且可以单击确定。ops,如果你给teamviewer,很难阅读整个代码。可以调试和检查