//模糊查询Combobox public void selectCombobox(ComboBox cb, List<string> listOnit) { //输入key之后返回的关键词 List<string> listNew = new List<string>(); //清空combobox cb.Items.Clear(); //清空listNew listNew.Clear(); //遍历全部备查数据 foreach (var item in listOnit) { if (item.ToLower().Contains(cb.Text.ToLower())) { //符合,插入ListNew listNew.Add(item); } } //combobox添加已经查询到的关键字 cb.Items.AddRange(listNew.ToArray()); //设置光标位置,否则光标位置始终保持在第一列,造成输入关键词的倒序排列 cb.SelectionStart = cb.Text.Length; //保持鼠标指针原来状态,有时鼠标指针会被下拉框覆盖,所以要进行一次设置 Cursor = Cursors.Default; }
private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { var EditingControl = e.Control as ComboBox; if (EditingControl != null) { //用个集合成员保持对combox的引用 if (ListCbx.Contains(EditingControl)) { EditingControl = ListCbx.Find(x => x == EditingControl); } else { EditingControl.Items.Clear(); EditingControl.Items.AddRange(new String[] { "Beijing", "Shanghai", "Shanghaiw", "Shangwhai", "Shawnghai", "Sheenghai", "Shangwwhai", "Guangzhou", "Wuhan", "李斌一", "李斌二", "Wuha", "Shenzhen" }); ListCbx.Add(EditingControl); EditingControl.TextUpdate += EditingControl_TextChanged; EditingControl.KeyDown += EditingControl_KeyDown; } } }
【winform】解决datagridview里放combox,combox不能按下键快速选择的问题
原文:https://www.cnblogs.com/HelloQLQ/p/14902296.html