Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动完成组合框C_C#_Winforms_Excel_Autocomplete - Fatal编程技术网

C# 自动完成组合框C

C# 自动完成组合框C,c#,winforms,excel,autocomplete,C#,Winforms,Excel,Autocomplete,我正在从excel将大约2000个项目的巨大数据库加载到combobox。例如CD标题。然后我从这个2000中选择了一张CD标题。我想在这里使用自动完成,但我不知道如何 // Loading items from Excel for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++) { for (cCnt = 1; cCnt < 2; cCnt++)

我正在从excel将大约2000个项目的巨大数据库加载到combobox。例如CD标题。然后我从这个2000中选择了一张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);
            }

在表单上,需要为组合框设置两个属性:

应该是Suggest、Append或SuggestAppend。我推荐SuggestAppend


应该是ListItems。

除了设置John在大约10分钟前引用的属性外,下面是一些用于数据绑定组合框的代码:

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();
}

这种方法适合你

    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
        {
        }

    }
并根据需要从数据库中实现GetColumnData_