Combobox项目问题 - 空白项目

时间:2016-02-29 21:17:40

标签: c# combobox datasource

在Windows窗体项目,Visual Studio 2010,.NET4,Windows 7 x64中,我无法找到以下两个依赖组合框的问题的答案。

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<string> items1 = new List<string>() { "", "1 Item", "3 Items", "5 Items" };
            comboBox1.DataSource = items1;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> items2 = new List<string>();
            switch (comboBox1.Text)
            {
                case "": 
                    break;
                case "1 Item":
                    items2.AddRange(new string[] { "I1" });
                    break;
                case "3 Items":
                    items2.AddRange(new string[] { "I1", "I2", "I3" });
                    break;
                case "5 Items":
                    items2.AddRange(new string[] { "I1", "I2", "I3", "I4", "I5" });
                    break;
            }
            comboBox2.DataSource = items2;
        }
    }
}

展开combobox2时,最初有一个&#34;空白项目&#34;。 但是当我选择&#34; 5项&#34;在combobox1中再次出现空白项目,在combobox2中有5个空白项目可见。 与&#34; 3项目相同&#34; - combobox2可容纳3个空白项目。

我尝试过使用不同的数据源(数组,BindingList,BindingSource)和其他方法(Items.Clear,将DataSource设置为null,清除DataBindings等),但没有成功。我发现的唯一解决方法是使用一个空元素绑定列表。

有没有人知道更好的方式来清除&#34;那空白的物品消失了吗?

0 个答案:

没有答案
相关问题