P
pmak
I try to implement a AutoComplete function to the datagridviewtextboxcolumn
to a Datagridview control with several others datagridviewtextboxcolumns.
This function is meant to apply to the column, CustomerID, in the
datagridview control. However the AutoComplete function occurs on EVERY
datagridviewtextbox 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.SqlClient
Public Class Form1
Dim scAutoComplete As New AutoCompleteStringCollection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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(strConn)
da = New SqlDataAdapter("Select * from [Orders]", conn)
da.Fill(ds, "Orders")
DataGridView1.DataSource = ds.Tables("Orders")
Dim cmd As New SqlCommand("Select CustomerID From customers", conn)
Dim dr As SqlDataReader
conn.Open()
dr = cmd.ExecuteReader
Do While dr.Read
scAutoComplete.Add(dr.GetString(0))
Loop
conn.Close()
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf
e.Control Is TextBox Then
With DirectCast(e.Control, TextBox)
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.CustomSource
.AutoCompleteCustomSource = scAutoComplete
End With
End If
End Sub
End Class
Thanks
to a Datagridview control with several others datagridviewtextboxcolumns.
This function is meant to apply to the column, CustomerID, in the
datagridview control. However the AutoComplete function occurs on EVERY
datagridviewtextbox 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.SqlClient
Public Class Form1
Dim scAutoComplete As New AutoCompleteStringCollection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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(strConn)
da = New SqlDataAdapter("Select * from [Orders]", conn)
da.Fill(ds, "Orders")
DataGridView1.DataSource = ds.Tables("Orders")
Dim cmd As New SqlCommand("Select CustomerID From customers", conn)
Dim dr As SqlDataReader
conn.Open()
dr = cmd.ExecuteReader
Do While dr.Read
scAutoComplete.Add(dr.GetString(0))
Loop
conn.Close()
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf
e.Control Is TextBox Then
With DirectCast(e.Control, TextBox)
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.CustomSource
.AutoCompleteCustomSource = scAutoComplete
End With
End If
End Sub
End Class
Thanks