1

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

我目前有一个方法,用数据库查询中的项目填充数组列表,然后返回用于填充组合框的数组列表。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;
    }

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

谢谢您的帮助!

4

1 回答 1

3

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

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

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

comboBox.Items.Insert(0, string.Empty);
于 2013-01-17T22:35:47.963 回答