是否可以将 DataTable 作为 TextBox 中的 AutoCompleteSource? (C#)
Jared 是正确的 - 如果不进行一些操作,就无法直接绑定。以下是使用 LINQ 数据集扩展检索字段作为自动完成源的示例:
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<System.Data.DataRow, String>(x => x.Field<String>("Title"))
.ToArray();
var source = new AutoCompleteStringCollection();
source.AddRange(postSource);
textBox1.AutoCompleteCustomSource = source;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)