如何将文本框上的自动完成链接到数据库表? C#2.0

时间:2022-09-02 14:48:36

I'm using Visual Studio 2005, C# with Framework 2.0. I'd like to use auto complete, but would like the list to come from a table in my database.

我正在使用Visual Studio 2005,C#和Framework 2.0。我想使用自动完成,但希望列表来自我的数据库中的表。

Is there a way to databind the AutoCompleteSoure?

有没有办法对AutoCompleteSoure进行数据绑定?

3 个解决方案

#1


You might want to take a look at this blogpost.

你可能想看一下这篇博文。

#2


You can accomplish what you want to do by using the AjaxControlToolkit AutoComplete

您可以使用AjaxControlToolkit AutoComplete完成您想要做的事情

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx

#3


Yes you can do it with auto-complete in WinForms in C#. Here is the example:

是的,您可以使用C#中的WinForms自动完成它。这是一个例子:

  1. Change the AutoCompleteMode of textbox to SuggestAppend.
  2. 将文本框的AutoCompleteMode更改为SuggestAppend。

  3. Change the AutoCompleteSource to CustomSource.
  4. 将AutoCompleteSource更改为CustomSource。

Now, write the following code in Enter_Event of textbox to load data from any table:

现在,在文本框的Enter_Event中编写以下代码以从任何表加载数据:

AutoCompleteStringCollection acs = new AutoCompleteStringCollection();
acs.Clear();

try
{
    this.Cursor = Cursors.WaitCursor;
    OleDbCommand odc = new OleDbCommand("<your sql statement>", <your connection>);
    OleDbDataReader odr = odc.ExecuteReader();

    while (odr.Read())
    {
        acs.Add(odr["name"].ToString());
    }

    textbox1.AutoCompleteCustomSource = acs;
}
catch (Exception ex)
{
    throw new ex;
}
finally
{
    this.Cursor = Cursors.Default;
}

Hope this code helps. Please reply for any other queries.

希望这段代码有帮助。请回复任何其他疑问。

#1


You might want to take a look at this blogpost.

你可能想看一下这篇博文。

#2


You can accomplish what you want to do by using the AjaxControlToolkit AutoComplete

您可以使用AjaxControlToolkit AutoComplete完成您想要做的事情

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx

#3


Yes you can do it with auto-complete in WinForms in C#. Here is the example:

是的,您可以使用C#中的WinForms自动完成它。这是一个例子:

  1. Change the AutoCompleteMode of textbox to SuggestAppend.
  2. 将文本框的AutoCompleteMode更改为SuggestAppend。

  3. Change the AutoCompleteSource to CustomSource.
  4. 将AutoCompleteSource更改为CustomSource。

Now, write the following code in Enter_Event of textbox to load data from any table:

现在,在文本框的Enter_Event中编写以下代码以从任何表加载数据:

AutoCompleteStringCollection acs = new AutoCompleteStringCollection();
acs.Clear();

try
{
    this.Cursor = Cursors.WaitCursor;
    OleDbCommand odc = new OleDbCommand("<your sql statement>", <your connection>);
    OleDbDataReader odr = odc.ExecuteReader();

    while (odr.Read())
    {
        acs.Add(odr["name"].ToString());
    }

    textbox1.AutoCompleteCustomSource = acs;
}
catch (Exception ex)
{
    throw new ex;
}
finally
{
    this.Cursor = Cursors.Default;
}

Hope this code helps. Please reply for any other queries.

希望这段代码有帮助。请回复任何其他疑问。