comboBox筛选下拉框的信息(下拉框信息模糊查询)

  1. 创建全局变量列表
 List<string> partListOld = new List<string>();
  1. 数据填充list,并将list中的数据填充到comboBox中
public void demo( DataTable dtJson){
                partList.Clear();  //清空列表中原有的数据,避免数据重复
                partList.Add(""); //添加空白行
                for (int i = 0; i < dtJson.Rows.Count; i++)
                {
                    partList.Add(dtJson.Rows[i]["udf01"].ToString().Trim() + "  " + dtJson.Rows[i]["udf02"].ToString().Trim());
                }
                comboBox3.Items.AddRange(partList.ToArray());
}
  1. 编写 comboBox1_TextUpdate事件
 private void comboBox1_TextUpdate(object sender, EventArgs e)
        {
            string s = this.comboBox1.Text;  //获取cb_material控件输入内容

            List<string> strList = new List<string>();   //存放原始数据(可以是对象,字符串...)
            strList.AddRange(partListOld.ToArray());  // List<string> materials
            List<string> strListNew = new List<string>();
            //清空combobox
            this.comboBox1.Items.Clear();
            //遍历全部原始数据
            foreach (var item in strList)
            {
                // 根据输入的值模糊查询,将符合条件的值存储到新strListNew的集合里面
                if (item.Contains(s))
                {
                    strListNew.Add(item);
                }
            }
            if (strListNew.Count >= 1) // 存在符合条件的内容
            {
                //将符合条件的内容加到combobox中
                this.comboBox1.Items.AddRange(strListNew.ToArray());
            }
            else  // 不存在符合条件时
            {
                // 下列代码为当查询不到符合的条件时新增自身输入的值
                this.comboBox1.Items.Add(this.comboBox1.Text);
            }
            //设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
            this.comboBox1.SelectionStart = this.comboBox1.Text.Length;  // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
            Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置
            this.comboBox1.DroppedDown = true; // 自动弹出下拉框
        }
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值