我遇到的问题是我无法在表单上看到任何控件。在我看到comboBox之前,但在运行程序之后,它现在在设计器视图中不可见。此外,如果我尝试添加任何内容,点击时不会生成任何事件。例如。如果我添加一个文本框,我无法访问textchanged事件或任何其他事件。我不确定发生了什么,但我附上了我的下面的函数,它是包含我唯一代码的“Form1_Load”。
{
{
try
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Carimed_Inventory;Integrated Security=True";
SqlConnection con2 = new SqlConnection(connectionString);
con2.Open();
string query = "SELECT * FROM dbo.Carimed; ";
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
string cari_des = dr2.GetString(dr2.GetOrdinal("Item_Description"));
suggestComboBox1.Items.Add(cari_des);
}
con2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
};
// TrySomeThings(); // <-- comment this to see the standard behavior
//suggestComboBox1.SelectedIndex = -1;
}
}
答案 0 :(得分:0)
原来是我的错。我有一个我工作的.cs文件,它覆盖了默认的winforms组合框,这反过来影响了它的可见性和其他要添加的控件。令人惊讶的是,修复它只是从form1_load中删除代码,该代码用值填充我的comboBox并将其放在一个函数中,因此这是新创建的函数。
void fillCari()//fill dropdown with values
{
try
{
string connectionString = "Data Source=CMDLAP126;Initial Catalog=Carimed_Inventory;User ID = sa; Password = 123456;";
SqlConnection con2 = new SqlConnection(connectionString);
con2.Open();
string query = "SELECT DISTINCT Item_Description FROM dbo.Carimed"; //select Convert(nvarchar(50),Item_Description)+ ':' +Convert(nvarchar(50),Item#) as Combined from Carimed
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
string cari_des = dr2.GetString(dr2.GetOrdinal("Item_Description"));
suggestComboBox1.Items.Add(cari_des);
suggestComboBox1.Text.Trim();
}
//con2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}