Auto Complete For a TextBox Control in DataGridView


In This article, I will explain the code snippet for Auto Complete a TextBox Control in DataGridView . It may be useful for learning the concept of TextBox Control in DataGridView.

Hi,

A Small Article Which Show you how to Show Tool tip for a Column Index of 1 in DataGridView

Before Going To the Main Function(s) we need to do some Small Manipulations to GridView
1. Grid View should have Enable Editing to True
2. Grid View Should have EditControlShowing Event

After doing that through Design now comes to code part

Declare a Variable Gobally



Private scAutoComplete As New AutoCompleteStringCollection



in Form_Load Event am Calling a Function Called bind Grid and let's have a look how bind Grid Function


Private Sub bindGrid()
DataGridView1.DataSource = getTable() ' To get Data Table for the Grid View
setAutoComplete()
End Sub

Private Sub setAutoComplete()
For i As Integer = 1 To 5
scAutoComplete.Add("Praveen " & CStr(i)) ' For Sample i added my name here you can load values from database
Next
End Sub



so in Form Load we binded data to Grid View and We gave values to Auto Complete Extender also

Now see the Code for Gridview Editing Controls Showing

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf e.Control Is TextBox Then ' Checking Whether the Editing Control Column Index is 1 or not if 1 Then Enabling Auto Complete Extender
With DirectCast(e.Control, TextBox)
.AutoCompleteCustomSource = scAutoComplete
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.CustomSource
End With
Else ' we are not Enabling Auto Complete Extendar
With DirectCast(e.Control, TextBox)
.AutoCompleteMode = AutoCompleteMode.None
End With
End If
End Sub


if you want to show for more than a column then take another AutoCompleteStringCollection Variable and assign to that autocompletecustomsource

hope this will help some one


Attachments

  • AutoCompleteTextboxinDataGridView (29740-291138-AutoComplete.zip)
  • Comments

    Author: Kastriot19 Jan 2010 Member Level: Bronze   Points : 1

    Wonderful code really!

    The only thing I don't see how to implement is to limit the number of suggestions, i.e.: you press "pr" and the code to display 3 only and not all of them in the list because the list may be huge...

    Any ideas on this?

    Regards

    Author: Kastriot19 Jan 2010 Member Level: Bronze   Points : 1

    Wonderful code really!

    The only thing I don't see how to implement is to limit the number of suggestions, i.e.: you press "pr" and the code to display 3 only and not all of them in the list because the list may be huge...

    Any ideas on this?

    Regards

    Author: Praveen19 Jan 2010 Member Level: Gold   Points : 1

    Hi Kastriot,

    for that you need to add Key Down event for the textbox and in that Event get the value from the db or any location and add to autocomplete extender object.



    Guest Author: mushtauqe20 Dec 2011

    Really helpful
    Thank you...

    Guest Author: shreyas.25.413 Dec 2012

    i have implemented but when the user presses enter to select value from auto complete list the cell focus moves to next cell it shouldnt happen

    Author: Praveen29 Jan 2013 Member Level: Gold   Points : 2

    Hi,

    For me I can able to select value from auto complete using key board. After selecting value from Auto complete and presses enter moving focus to next cell. I just downloaded the attachment and ran that application. It is working fine.

    What problem you are facing. could you brief about that.

    Regards,
    Praveen

    Guest Author: vijayakumara nv19 Feb 2013

    Hello sir..
    This code really help me..
    and one more issue here is,

    the retrieved collection of data(i.e 'scAutoComplete' in above code) is applicable not only for particular column cells but also for other columns cells when cells clicked.

    how to do that..?

    Thank you,

    Guest Author: Vivek11 Apr 2013

    How to work with data from database. in this
    Private Sub setAutoComplete()
    For i As Integer = 1 To 5
    scAutoComplete.Add("Praveen " & CStr(i)) ' For Sample i added my name here you can load values from database
    Next
    End Sub


    i have uploaded data in in gridview from database in form load event... you code is working for me . but i don't know how to work data from database



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: