0
我正在处理visual studio 2012 c#... 我将值插入combox中...我从数据库中取出它们...我想知道如何将一个项目添加到组合框中...虐待显示ü下面的代码:插入数据源combobox
下面这个函数,以填补containig名称和ID从表中取数据库名称组合框:
List<Lookup> fillCombo(string query, string column)
{
List<Lookup> lookups = new List<Lookup>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Lookup lookupobject = new Lookup();
lookupobject.ID = Convert.ToInt32(reader["ID"]);
//if (reader["Name"] != DBNull.Value)
lookupobject.Name = reader[column].ToString();
lookups.Add(lookupobject);
}
conn.Close();
}
return lookups;
}
然后我调用这个函数如下:
lookups = fillCombo("select id,name from LookupDetails where LOOKUPID = (select id from Lookup where Name = 'users')", "name");
comboBox2.DataSource = lookups;
comboBox2.DisplayMember = "name";
什么是不工作?这是wpf/winforms? 'lookups'是否包含任何值? – Sayse 2014-09-19 07:58:26
我不能添加项目..我尝试使用此命令添加项目:comboBox2.Items.Insert(0,“”); 但它没有工作cz的数据绑定..所以我想知道如何将数据添加到组合框...是列表包含对象列表有每个对象有用户名和编号 – mhammadkassem 2014-09-19 08:12:28
你需要插入'lookups' – Sayse 2014-09-19 08:13:29