在datagridview中自动完成C# [英] Auto complete in datagridview C#

查看:95
本文介绍了在datagridview中自动完成C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我到处寻找问题的答案。但我还没有找到满意的答案。现在这里是我的问题。



我在C#中有一个datagridview,它对任何数据源都没有绑定。我想让用户在其中一列中输入数据。我需要做的是支持用户自动完成。



自动完成的数据来自数据库。我可以很好地处理这个问题,但我的问题是建议应该取决于用户输入的字符。

例如,如果用户输入''g'并且我的数据库查询返回''garlic'',则自动完成应该显示它。



对于普通文本框来说这很容易。但是datagridview的问题在于我无法读取用户输入时输入的字符。编辑完成后,单元格值更改事件将触发。数据库中的数据太多,无法立即添加到自动完成源。所以我必须决定获取用户输入的内容并根据它生成源。



有什么方法可以实现这个任务吗?请帮忙。

Hi,

I''ve been searching for an answer for my question everywhere. But I couldn''t find a satisfactory answer yet. Now here''s my problem.

I have a datagridview in C# which is unbound to any data source. I want to let the user to enter data in one of the columns. What I need to do is to support the user with autocomplete.

The data for auto complete comes from a database. I could well handle this but my problem is that the suggestions should depend on the characters entered by the user.
For an example if the user enters ''g'' and my database query returns ''garlic'', the auto complete should show it.

This is quite easy for normal textboxes. But the problem with datagridview is that I cannot read the characters users enter as they type in. The cell value changed event fires after the editing is complete. The data in the database is too much to be added to the autocomplete source at once. So I have to resolve to get what the user type and generate the source according to that.

Is there any way that I could achieve this task? Please help.

推荐答案

这是我使用的代码。这对我有用。

只需添加更多 If -statements即可完成多个列。我的DGV中有几个根据以前的改变而变化,所以它非常灵活。

Here is the code I use. This works for me.
You can do more than one column by just adding more If-statements. I have several in my DGV that change depending on the previous ones so it is pretty flexible.
private void dgvCheckEntry_EditingControlShowing(Object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            
            
            if (dgvCheckEntry.CurrentCell.ColumnIndex == 1)
            {
                SqlDataReader dreader;
                SqlConnection conn = new SqlConnection(conStrn2);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                AutoCompleteStringCollection acBusIDSorce = new   AutoCompleteStringCollection();
                cmd.CommandText = "Select BusinessID from tblBusinessID";
                conn.Open();
                dreader = cmd.ExecuteReader();
                if (dreader.HasRows == true)
                {
                    while (dreader.Read())
                        acBusIDSorce.Add(dreader["BusinessID"].ToString());
                }
                else
                {
                    MessageBox.Show("Data not Found");
                }
                dreader.Close();

                 
                //ComboBox txtBusID = e.Control as ComboBox;
                TextBox txtBusID = e.Control as TextBox;
                if (txtBusID != null)
                {
                    txtBusID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    txtBusID.AutoCompleteCustomSource = acBusIDSorce;
                    txtBusID.AutoCompleteSource = AutoCompleteSource.CustomSource;

                }
            }
}


试试这个: -



您可以使用2个事件,一个是Datagridview CellBeginEdit事件,另一个是DataGirdView Keypress事件。 KeyPress事件对你更好,因为当单元格中的任何Character被占用时会引发它,但是当光标进入datagridview中的单元格时会引发DatagridView CellBeginEdit事件。
Try This:-

You can use 2 events one is Datagridview CellBeginEdit event and other is DataGirdView Keypress event. KeyPress event is better for you because it is raised when any Character is enetered in cell, but DatagridView CellBeginEdit event is raised when the cursor enters the cell in datagridview.


这篇关于在datagridview中自动完成C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆