Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Autocomplete
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 17-Aug-12 22:00pm
Edited 17-Aug-12 22:08pm
v2
Rate this: bad
good
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";
                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;
 
                }
            }
}
  Permalink  
v2
Comments
srigates at 24-Oct-13 11:21am
   
Thanks a lot..............
by srigates....
srigates at 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?
Member 8780640 at 2-Feb-14 13:07pm
   
Thank you very Much.......
By Rajan
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,580
1 OriginalGriff 8,370
2 Peter Leow 4,859
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
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