【问题标题】:C# window form Combobox drop down and sugguest appened list on text written in comboboxC#窗口表单组合框下拉并建议在组合框中写入的文本上附加列表
【发布时间】:2026-02-11 08:10:01
【问题描述】:

我有一个从数据库绑定的组合框,现在我在组合框中写入文本时想要下拉列表,并从列表中选择与组合框中写入的文本匹配的数据。 组合框绑定成功,但在它的文本更改时,自动列表不是按照书面文本的下拉列表。请建议我如何做到这一点。 下面给出了我的绑定源

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mateenwin
{
public partial class Sale : Form
{
    public Sale()
    {
        InitializeComponent();
        PaymentType_bind();
        Customer_bind();

    }
  /// combobox bind method

  public void PaymentType_bind()
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mateenwin;User ID=sa;Password=123");
        SqlCommand command = new SqlCommand("Select * from PaymentType", con);
        SqlDataAdapter adp = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        this.comboBox1.DataSource = dt;
        this.comboBox1.DisplayMember = "Payment_Type";
        this.comboBox1.ValueMember = "Payment_Type_ID";
    }

【问题讨论】:

    标签: c# combobox windows-forms-designer


    【解决方案1】:

    ComboBox 有一个名为 AutoCompleteMode 的属性。它建议一个以书面文本开头的价值。我向你推荐了那个。还将 AutoCompleteSource 设置为您的数据库。

    【讨论】:

    • DropDownStyle= DropDown
    • 所以你不希望它完成文本。只是一个具有适当值的列表?