I try to implement a AutoComplete function to the datagridviewtex tboxcolumn
to a Datagridview control with several others datagridviewtex tboxcolumns.
This function is meant to apply to the column, CustomerID, in the
datagridview control. However the AutoComplete function occurs on EVERY
datagridviewtex tbox column with the same datatype - string. For example the
function should be at the CustomerID column which has the column index =1,
however the function is appeared on columns, Ship Name, ShipAddress, ShipCity
and ShipRegion, where the datatype is string.
Here is the code I found at
http://www.vb-tips.com/DataGridViewAutoComplete.aspx
and I created the sample datagridview control to test the function:
DataGridView: Auto Complete Textbox
In the datagridview's editing control showing event you have better access
to the textboxes properties. Here is an example of adding autocomplete to
the textbox.
Imports System.Data.Sql Client
Public Class Form1
Dim scAutoComplete As New AutoCompleteStr ingCollection
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim ds As New DataSet
strConn = "Server = .;Database = NorthWind; Integrated Security =
SSPI;"
conn = New SqlConnection(s trConn)
da = New SqlDataAdapter( "Select * from [Orders]", conn)
da.Fill(ds, "Orders")
DataGridView1.D ataSource = ds.Tables("Orde rs")
Dim cmd As New SqlCommand("Sel ect CustomerID From customers", conn)
Dim dr As SqlDataReader
conn.Open()
dr = cmd.ExecuteRead er
Do While dr.Read
scAutoComplete. Add(dr.GetStrin g(0))
Loop
conn.Close()
End Sub
Private Sub DataGridView1_E ditingControlSh owing(ByVal sender As Object,
ByVal e As System.Windows. Forms.DataGridV iewEditingContr olShowingEventA rgs)
Handles DataGridView1.E ditingControlSh owing
If DataGridView1.C urrentCell.Colu mnIndex = 1 AndAlso TypeOf
e.Control Is TextBox Then
With DirectCast(e.Co ntrol, TextBox)
.AutoCompleteMo de = AutoCompleteMod e.SuggestAppend
.AutoCompleteSo urce = AutoCompleteSou rce.CustomSourc e
.AutoCompleteCu stomSource = scAutoComplete
End With
End If
End Sub
End Class
Thanks