2015-04-23 78 views
0

我有这样的一段代码自动完成我的搜索文本框化妆自动完成文本框形成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; 
    } 

建议请

+0

根据我的一切适当的.. –

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