是否可以在TextBox中将DataTable作为AutoCompleteSource?(C#)
Jared是正确的 - 如果不做一些操作就不能直接绑定.以下是使用LINQ DataSet扩展来检索字段作为自动完成源的示例:
DataTable dtPosts = new DataTable(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StackOverflow"].ConnectionString)) { conn.Open(); using (SqlDataAdapter adapt = new SqlDataAdapter("SELECT TOP 100 Id, Title, Body, CreationDate FROM Posts WHERE Title IS NOT NULL ORDER BY Id", conn)) { adapt.SelectCommand.CommandTimeout = 120; adapt.Fill(dtPosts); } } //use LINQ method syntax to pull the Title field from a DT into a string array... string[] postSource = dtPosts .AsEnumerable() .Select(x => x.Field ("Title")) .ToArray(); var source = new AutoCompleteStringCollection(); source.AddRange(postSource); textBox1.AutoCompleteCustomSource = source; textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;