是否有可能使一个DataTable作为一个TextBox一个AutoCompleteSource? (C#) [英] Is it possible to make a DataTable as a AutoCompleteSource in a TextBox? (C#)

查看:639
本文介绍了是否有可能使一个DataTable作为一个TextBox一个AutoCompleteSource? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使一个DataTable作为一个TextBox一个AutoCompleteSource? (C#)

Is it possible to make a DataTable as a AutoCompleteSource in a TextBox? (C#)

推荐答案

贾里德是正确的 - 你不能直接没有做一些处理绑定。下面是使用LINQ数据集扩展检索字段作为自动完成源的例子:

Jared is correct - you can't bind directly without doing some manipulation. Here's an example of using the LINQ DataSet extensions to retrieve a field as the autocomplete source:

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;

这篇关于是否有可能使一个DataTable作为一个TextBox一个AutoCompleteSource? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆