c# comboBox模糊匹配,
1.系统提供了顺序的匹配方式。
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
本方法的确定是必须是从前往后依次匹配。如:数据项中有“中国人民”,想输入“人民”查询,结果为空。
2.自己动手做匹配:vs2008
dtModel = new DataTable(); string sql1 = "SELECT REPORT_TEMPLATE_CODE ,REPORT_TEMPLATE_NAME FROM REPORT_TEMPLATE WHERE BARGAIN_FORMAT='0' "; SystemInfo.dbData.Retrieve(sql1, dtModel);
foreach (DataRow dr in dtModel.Rows) { comboBox1.Items.Add(new ReportName(dr["REPORT_TEMPLATE_CODE"].ToString(), dr["REPORT_TEMPLATE_NAME"].ToString())); } comboBox1.TextUpdate+=new EventHandler(comboBox1_TextUpdate);
private void comboBox1_TextUpdate(object sender, EventArgs e) { string key = this.comboBox1.Text; DataTable dttemp = dtModel.Clone(); comboBox1.Items.Clear(); if (!string.IsNullOrEmpty(key)) { DataRow[] drs = dtModel.Select(string.Format(" REPORT_TEMPLATE_NAME like '%{0}%' ", key)); if (drs != null && drs.Length > 0) { foreach (DataRow drow in drs) { dttemp.Rows.Add(drow.ItemArray); } } } else { dttemp=dtModel; } foreach (DataRow dr in dttemp.Rows) { comboBox1.Items.Add(new ReportTemplet(dr["REPORT_TEMPLATE_CODE"].ToString(), dr["REPORT_TEMPLATE_NAME"].ToString())); } comboBox1.Select(comboBox1.Text.Length, 0); comboBox1.DroppedDown = true; Cursor = Cursors.Default; }
public class ReportName { public ReportName(string id, string name) { this._id = id; this._name = name; } private string _name; public string Name { get { return _name; } set { _name = value; } } private string _id; public string Id { get { return _id; } set { _id = value; } } public override string ToString() { return this.Name; } }