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

用户C-137

所以我有一个DataGrid使用来自MySQL数据库的数据。我正在尝试使其在用户开始在“项目”列中键入时,该框将自动填充/建议/附加,并允许在数据库列中的所有元素的下拉列表。

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

因此,我发表了这篇文章。如果有人有任何建议会有所帮助。以下是我用来执行自动填充和下拉列表的一些功能。

     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;
    }

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

米奇

我正在尝试使其在用户开始在“项目”列中键入时,该框将自动填充/建议/附加,并允许在数据库列中的所有元素的下拉列表。

到目前为止,我要么设法提供了自动填充功能,要么提供了下拉列表。我似乎无法让他们两个一起工作

这里重要的是您希望用户能够附加因此,下拉控件不得下拉列表,因为这将阻止用户输入列表中尚不存在的内容。我建议您将其更改为常规的 下拉控件。

典型的流程是您:

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

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

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

在打开C#文件的Visual Studio中,您可以非常容易地看到这种模式,并观察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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章