使用 tableadapter windows 表单 C# 制作自动完成文本框

make autocomplete textbox using tableadapter windows forms C#

我有这段代码可以自动完成我的搜索文本框

AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
        DataTableReader reader=this.customerTableAdapter.GetData().CreateDataReader();
        while(reader.Read()){
            coll.Add(reader.GetString(0));
        }

        search.AutoCompleteCustomSource = coll;

这是执行它的最佳方式吗?或者是否有使自动完成源直接成为列本身的功能?

此外,这段代码只过滤名字,但是当我将这段代码与 gridview 一起使用时,它为我提供了更好的搜索能力,因此它可以捕获名字的任何部分

private void search_KeyUp(object sender, KeyEventArgs e)
    {
        string outputInfo = "";
        string[] keyWords = search.Text.Split(' ');

        foreach (string word in keyWords)
        {
            if (outputInfo.Length == 0)
            {
                outputInfo = "(Name LIKE '%" + word + "%')";
            }
            else
            {
                outputInfo += " AND (Name LIKE '%" + word + "%')";
            }
        }

        //Applies the filter to the DataView
        myView.RowFilter = outputInfo;
    }

请多多指教

这段代码没有使用循环就完成了,它填充了字符串数组中 table 的一列,然后用作 autoCompleteSource

            DataTable dt = this.data_allDataSet.merchandise;
        //use LINQ method syntax to pull the Title field from a DT into a string array...
        string[] postSource = dt
                            .AsEnumerable()
                            .Select<System.Data.DataRow, String>(x => x.Field<String>("name"))
                            .ToArray();

        var source = new AutoCompleteStringCollection();
        source.AddRange(postSource);
        cat_name.AutoCompleteCustomSource = source;
        cat_name.AutoCompleteMode = AutoCompleteMode.Suggest;
        cat_name.AutoCompleteSource = AutoCompleteSource.CustomSource;