开发者

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

Is it possible to make a DataTable as a AutoCompleteSource in a TextBox? (开发者_JAVA百科C#)


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;


Not directly, you'll want to read your datatable into an AutoCompleteStringCollection.

You could do something like this (from here):

private AutoCompleteStringCollection GetAutoSourceCollectionFromTable(DataTable table)
{
    AutoCompleteStringCollection autoSourceCollection = new AutoCompleteStringCollection();

    foreach (DataRow row in table.Rows)
    {
        autoSourceCollection.Add(row[0]); //assuming required data is in first column
    }

    return autoSourceCollection;
}


I guess you already know your answer, but to someone who comes to this question, the answer is no, if you are in doubt, see the supported options for AutoCompleteSource.

When it comes to AutoComplete custom data in Winforms, we use the AutoCompleteCustomSource option which can only get or set an AutoCompleteStringCollection.

Now, you can populate a DataTable by fetching records from a database with your desired query and method, and then you can add these records to the StringCollection by iterating through the DataRow elements of the DataTable. Here's an easy example made by Leandro Tuttini:

textBox1.AutoCompleteCustomSource = LoadAutoComplete(); //this method is below
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;


public static AutoCompleteStringCollection LoadAutoComplete()
{
   DataTable dt = LoadDataTable(); //suppose this method returns a DataTable with fetched records from database.
   AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();
   foreach (DataRow row in dt.Rows)
   {
      stringCol.Add(Convert.ToString(row[0]));
   }
   return stringCol; //return the string collection with added records
}


Yes.

Here is an example using a typed dataset and two textboxes (source...scroll past the large images at the top to get to the code)

For Each store As StoreDataSet.StoreRow In StoreDataSet.Store
    If store.StoreID.StartsWith("0") Then
        StoreIDTextBox.AutoCompleteCustomSource.Add(store.StoreID.TrimStart("0"))
    End If
    StoreIDTextBox.AutoCompleteCustomSource.Add(store.StoreID)
    StoreNameTextBox.AutoCompleteCustomSource.Add(store.StoreName)
Next store
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜