Click here to Skip to main content
12,559,868 members (49,043 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Autocomplete

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 17-Aug-12 21:00pm
Updated 17-Aug-12 21:08pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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";
                dreader = cmd.ExecuteReader();
                if (dreader.HasRows == true)
                    while (dreader.Read())
                    MessageBox.Show("Data not Found");
                //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;
srigates 24-Oct-13 11:21am
Thanks a lot..............
by srigates....
srigates 23-Nov-13 13:11pm
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
SqlCommand namecmd = new SqlCommand("select Item_name,Item_code,Item_mrsp from Item_table where Item_Active=" + 1 + " order by Item_name ASC", con);
here i am want bind a Keyed text by Like "gridviewcolumn2.value%".
i want use a like query in the select statement.How?
Member 8780640 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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161026.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100