【问题标题】:Adding Null Value to ComboBox bound to ArrayList将空值添加到绑定到 ArrayList 的 ComboBox
【发布时间】:2013-01-01 13:03:51
【问题描述】:

我已经看到了一些与我遇到的这个问题类似的问题,但它们似乎都没有解决。

我目前有一个方法可以使用来自数据库查询的项目填充数组列表,然后返回用于填充组合框的数组列表。 ComboBox 将用作其他数据库查询的搜索过滤器。

我想在数组列表中添加一个空值,以便在选择空值时允许用户“关闭过滤器”。

将空值添加到数组列表时出现问题。当我在添加数据库项目之前添加空值时,所有项目都不会显示在实际的 ComboBox 中。当我在填充 ArrayList 后添加空值时,空值永远不会在 ComboBox 中显示为一行,但其他所有内容都在那里。

        public static ArrayList DropDown()
    {
        ArrayList status = new ArrayList();
        connect.Open();

        // Finds the stuff to use in the  COMBO BOX
        cmd.CommandText = String.Format("query");
        reader = cmd.ExecuteReader();

        while (reader.Read())
        {

            status.Add(String.Format("{0}", reader[BANK_ID]));
        }
        string holder = null;
        status.Add(holder); 
        connect.Close();
        return status;
    }

这导致填充的 ComboBox,但没有空行。

        public static ArrayList DropDown()
    {
        ArrayList status = new ArrayList();
        string holder = null;
        status.Add(holder); 
        connect.Open();

        // Finds the stuff to use in the  COMBO BOX
        cmd.CommandText = String.Format("query");
        reader = cmd.ExecuteReader();
        ;
        while (reader.Read())
        {

            status.Add(String.Format("{0}", reader[BANK_ID]));
        }

        connect.Close();
        return status;
    }

这会导致组合框中没有任何内容。

感谢您的帮助!

【问题讨论】:

  • 您是否考虑过将字符串“null”添加到组合框中?然后,当您使用组合框作为搜索条件时,将您的字符串值“null”替换为 null 的 sql 值。请注意 Sql Server 中的 '=' 与 'is'。每次都能得到我('= null' 不起作用)
  • 在添加 null 之前清除您的 ArrayList。
  • 如果您手动创建 ArrayList 而不查询数据库,它是否仍然显示相同的行为?
  • 手动创建arraylist 时会发生相同的行为。另外,我不确定在添加 null 之前清除 arraylist 会有什么帮助。无论如何我都这样做了,但结果相同=/。除非万不得已,否则我宁愿不使用字符串 null 并强制转换它。

标签: c# winforms oracle


【解决方案1】:

组合框的项目集合不能包含空值。参考: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx

我假设设置 DataSource 是在内部检查这一点,因此您注意到的行为(可能更知情的人可以确认这一点)

我建议您添加 String.Empty ,您可以一起摆脱 arraylist 并使用字符串数组/列表。

comboBox.Items.Insert(0, string.Empty);

【讨论】:

  • 啊这更有意义...感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
相关资源
最近更新 更多