使用tableadapter windows窗体C#创建自动完成文本框

时间:2015-04-23 06:31:29

标签: c# winforms autocomplete tableadapter

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

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

建议

1 个答案:

答案 0 :(得分:0)

这段代码在不使用循环的情况下完成了这一操作,它在字符串数组中填充表的一列,然后用作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;