【问题标题】:How do I link autocomplete on a textbox to a database table? C# 2.0如何将文本框上的自动完成链接到数据库表? C# 2.0
【发布时间】:2013-11-18 02:46:13
【问题描述】:

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

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

【问题讨论】:

  • 您确实应该指定 WinForms、WPF、WebForms 等。

标签: c# data-binding textbox autocomplete


【解决方案1】:

您可能想看看这个blogpost

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      是的,您可以在 C# 中使用 WinForms 中的自动完成功能来实现。示例如下:

      1. 将文本框的AutoCompleteMode更改为SuggestAppend
      2. AutoCompleteSource 更改为CustomSource

      现在,在文本框的 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;
      }
      

      希望此代码有所帮助。如有其他疑问,请回复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2019-07-13
        • 1970-01-01
        相关资源
        最近更新 更多