- 创建全局变量列表
List<string> partListOld = new List<string>();
- 数据填充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());
}
- 编写 comboBox1_TextUpdate事件
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
string s = this.comboBox1.Text;
List<string> strList = new List<string>();
strList.AddRange(partListOld.ToArray());
List<string> strListNew = new List<string>();
this.comboBox1.Items.Clear();
foreach (var item in strList)
{
if (item.Contains(s))
{
strListNew.Add(item);
}
}
if (strListNew.Count >= 1)
{
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;
}