简体   繁体   English

自动完成ComboBox C#

[英]AutoComplete ComboBox C#

i'm loading huge database from excel (about 2000 items) to combobox. 我正在将巨大的数据库从excel(约2000项)加载到组合框。 For example CD titles. 例如CD标题。 Then i select 1 CD title from this 2000. I'd like to use here autocomplete, but i don't know how.. 然后,我从2000年中选择1张CD标题。我想在这里使用自动完成功能,但我不知道如何。

       // Loading items from Excel
       for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt < 2; cCnt++)
                {
                  str = Convert.ToString(saRet[rCnt,cCnt]);
                  // Loading items to ComboBox
                  ReferenceCombo.Items.Add(str);
            }

On your form, you need to set two properties for your ComboBox: 在表单上,​​您需要为ComboBox设置两个属性:

AutoCompleteMode should be Suggest, Append, or SuggestAppend. AutoCompleteMode应该为“ Suggest”,“ Append”或“ SuggestAppend”。 I recommend SuggestAppend. 我建议SuggestAppend。

AutoCompleteSource should be ListItems. AutoCompleteSource应该是ListItems。

this method does it for you 这个方法为你做

    private void LoadStuffNames()
    {

        try
        {
                string Query = "select stuff_name from dbo.stuff";
                string[] names = GetColumnData_FromDB(Query);

                comboName.AutoCompleteMode = AutoCompleteMode.Suggest;
                comboName.AutoCompleteSource = AutoCompleteSource.CustomSource;
                AutoCompleteStringCollection x = new AutoCompleteStringCollection();
                if (names != null && names.Length > 0)
                    foreach (string s in names)
                        x.Add(s);

                comboName.AutoCompleteCustomSource = x;
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }

and implement "GetColumnData_FromDB" as you wish 并根据需要实现“ GetColumnData_FromDB”

In addition to setting the property referenced by John about 10 minutes ago, here is some code that I use to databind my combo box: 除了设置约10分钟前John引用的属性外,以下是一些用于数据绑定组合框的代码:

static BindingSource jp2bindingSource = new BindingSource();

void jp2FillCombo() {
  ComboBox comboBox1 = new ComboBox();
  comboBox1.Items.Clear();
  object[] objs = jp2Databind(new DataSet(), "Table1", "Column1", true);
  comboBox1.Items.AddRange(objs);
}

static object[] jp2Databind(DataSet dataset, string tableName, string columnName, bool unique) {
  jp2bindingSource.DataSource = dataset;
  jp2bindingSource.DataMember = tableName;
  List<string> itemTypes = new List<string>();
  foreach (DataRow r in dataset.Tables[tableName].Rows) {
    try {
      object typ = r[columnName];
      if ((typ != null) && (typ != DBNull.Value)) {
        string strTyp = typ.ToString().Trim();
        if (!String.IsNullOrEmpty(strTyp)) {
          if (unique) {
            if (!itemTypes.Contains(strTyp)) {
              itemTypes.Add(strTyp);
            }
          } else {
            itemTypes.Add(strTyp);
          }
        }
      }
    } catch (Exception err) {
      Global.LogError("Databind", err);
    }
  }
  try {
    itemTypes.Sort();
  } catch (Exception err) {
    Console.WriteLine(err.Message);
  }
  return itemTypes.ToArray();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM