是否可以有一个自动填充组合框(在数据网格视图中),它还提供列中所有数据库项目的下拉列表?

Is it possible to have an autofill combobox (within a datagridview) that also provides a dropdown list of all database items in a column?

所以我有一个 DataGrid,它使用来自 MySQL 数据库的数据。我试图做到这一点,以便当用户开始在 "Items" 列中键入时,该框将 autofill/suggest/append 以及允许数据库列中所有元素的下拉列表。

到目前为止,我已经设法提供 自动填充 功能 下拉列表 . 我似乎无法让它们一起工作。我开始怀疑这是否可能,但我还没有找到解决方案。

因此,我做了这个post。如果有人有任何建议会有所帮助。下面是我用来做自动填充和下拉列表的一些功能。

     private void invoice_DG_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        string titleText = invoice_DG.Columns[1].HeaderText;
        if (titleText.Equals("ITEM"))
        {
            ComboBox autoText = e.Control as ComboBox;
            /*if (autoText != null)
            {              
                autoText.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                //AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
                autoText.AutoCompleteCustomSource = get_From_Database();
                //autoText.Items.Add(get_From_Database());                    
            }*/

            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                autoText.DropDownStyle = ComboBoxStyle.DropDown;
                autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                autoText.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
                autoText.AutoCompleteCustomSource = get_From_Database();
            }
        }
    }

    private AutoCompleteStringCollection get_From_Database()
    {
        AutoCompleteStringCollection Coll = new AutoCompleteStringCollection();

        string querySelect = "SELECT * FROM Items";
        MySqlCommand commandSelect = new MySqlCommand(querySelect, conn);
        MySqlDataReader reader = commandSelect.ExecuteReader();
        while (reader.Read())
        {
            string type = reader.ToString();
            Coll.Add(type); //data inserted in collection so that it will be autocomplete when you type keywords
            if (list_Loaded == false)
            {
                string item = reader.GetString("name");
                ITEM.Items.Add(item);
            }
        }            reader.Close();
        list_Loaded = true;


        return Coll;
    }

注意 这些功能正在改变,可能无法反映最终被接受的内容。这只是他们的当前状态

I'm trying to make it so that when the user start's typing in the "Items" column the box will autofill/suggest/append as well as allow for a dropdown list of all the elements within a column of the database.

So far I've either managed to provide the autofill feature OR the dropdown list. I can't seem to get both of them to work together

这里重要的是您希望用户能够追加。因此,下拉控件必须 而不是 下拉列表 ,因为这将阻止用户输入不存在的内容列表。我建议您将其更改为 regular drop-down 控件。

典型的流程是你:

  1. 在第一次按键后显示下拉控件,并开始使用根据用户键入的内容过滤的值(无论是否来自数据库是偶然的)填充下拉列表。如果没有匹配项,则这是新条目的开始,因此输入的字符成为列表中的新条目

  2. 随着输入的字符越来越多,继续过滤或将字符附加到新条目(从#1 开始)

  3. 当他们按 Enter/焦点时,select 匹配的过滤记录或者如果它是一个新条目则适当保存(在这种情况下到数据库)

您可以在 Visual Studio 中很容易地看到这种模式,打开 C# 文件并观察 Intellisense 的自动完成方面如何与 C# 的 dynamic 关键字一起工作。通常 C# 自动完成类型会阻止您在无法识别该方法时自动完成方法调用。

dynamic 自动完成的行为不同,因为它不做任何假设,默认情况下不提供潜在成员。不过确实有.

的概念
dynamic something = // get a reference to some object
something.Foo ();   // Intellisense won't offer the Foo() suggestion.  
                    // It won't result as a compile error either

但是,如果我们尝试在 相同的 方法范围内再次调用相同的方法,Intellisense 会介入并向我们显示我们在 Foo() 之前调用的方法(至少在编码时间)可用。当然,直到运行时我们才能确定。

something.Foo();  // Foo() NOT available in auto-complete
something.Foo();  // Foo() now available in auto-complete