【问题标题】:inserting into data sourced combobox插入数据源组合框
【发布时间】:2014-09-19 07:56:58
【问题描述】:

我正在开发 Visual Studio 2012 C# ... 我将值插入到组合框中...我从数据库中获取它们...我想知道如何将项目添加到组合框中...我会向您展示以下代码:

这里这个函数用取自数据库中包含名称和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 是否包含任何值?
  • 我无法添加项目..我尝试使用以下命令添加项目:comboBox2.Items.Insert(0, "");但它没有工作cz数据已绑定..所以我想知道如何将数据添加到组合框...是的列表包含具有对象的列表每个对象都有用户名和ID
  • 你需要插入到lookups
  • @mhammadkassem。是winforms应用吗?
  • 你能告诉我怎么做吗? cz 我是 C# 的初学者,请你给我写代码?

标签: c# combobox


【解决方案1】:

设置 DataSource 属性时,无法修改 ComboxBox 项集合。

您可以选择修改List&lt;Lookup&gt; 或通过迭代List&lt;Lookup&gt; 将项目添加到组合框中。

这是使用 foreach 循环在组合框中添加项目并在组合框的 0 索引处插入项目的选项:

lookups = fillCombo(@"select id,name from LookupDetails where LOOKUPID =  
                     (select id from Lookup where Name = 'users')", "name");

foreach(var obj in lookups)
     comboBox2.Items.Add(obj);

comboBox2.DisplayMember = "Name";
comboBox2.Items.Insert(0, ""); 

注意:在问题中提到的代码中,SqlDataReader 从未关闭,我已通过包含using 语句对其进行了修改。当您在using 块中写入这些内容时,您不必关闭SqlDataReaderSqlConnection

List<Lookup> fillCombo(string query, string column)
{
    List<Lookup> lookups = new List<Lookup>();
    string sConstring = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(sConstring))
    using(SqlCommand cmd = new SqlCommand(query, conn))
    {
        conn.Open();
        using(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);
            }
         }
     }        

    return lookups;
}

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2015-06-30
    • 1970-01-01
    • 2017-08-27
    • 2011-12-05
    相关资源
    最近更新 更多