【发布时间】: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 并强制转换它。