Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 17-Aug-12 21:08pm
v2

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.
C#
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;

                }
            }
}
 
Share this answer
 
v2
Comments
srigates 24-Oct-13 11:21am    
Thanks a lot..............
by srigates....
srigates 23-Nov-13 13:11pm    
hi,
I am using Datagridview in C# win form,
here in the Second Column i want to bind a Autocompletion of text... Its work fine in the select query
<pre>
SqlCommand namecmd = new SqlCommand("select Item_name,Item_code,Item_mrsp from Item_table where Item_Active=" + 1 + " order by Item_name ASC", con);
</pre>
here i am want bind a Keyed text by Like "gridviewcolumn2.value%".
i want use a like query in the select statement.How?
rajanarora14988 2-Feb-14 13:07pm    
Thank you very Much.......
By Rajan
zhitu 23-Jul-15 5:22am    
i am using this auto complete textbox in datagridview and changed some in textbox like this
//retreiving data from query ..............
TextBox txtBusID = e.Control as TextBox;
txtBusID.font = new font("Zawgyi-One",10);
txtBusID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtBusID.AutoCompleteCustomSource = acBusIDSorce;
txtBusID.AutoCompleteSource = AutoCompleteSource.CustomSource;

then my autofill didn't work .
Please help me
Member 12133962 7-Dec-15 2:43am    
Thanks a lot.

Kishore
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900