SQL Server Autocomplete TextBox Column in DataGridView Stopped working

41ik7eoe  于 2023-03-17  发布在  其他
关注(0)|答案(2)|浏览(110)

I have a datagridview with one of its column as TextBox. I wrote a function to populate values from database and suggest values to autocomplete the text. I achieved it, and then I started coding to make a column auto increment (Sr.No) , so I wrote some more lines of code and changed some properties and all of a sudden the textbox stopped auto-completing. Now I tried every possible step to make it work but failed. I dont know what is the property that I changed affected this. I am putting my code here, please help

This is the code for Editingcontrolshowing event...

Private Sub DataGridView2_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView2.EditingControlShowing

    DataGridView2.BeginEdit(True)
    Dim autoText As TextBox = TryCast(e.Control, TextBox)
    If autoText IsNot Nothing Then
        autoText.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        autoText.AutoCompleteCustomSource = AutoCompleteLoad()
        autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
    End If
End Sub

This is the autocomplete function where I loaded values...

Public Function AutoCompleteLoad() As AutoCompleteStringCollection
    Dim str As AutoCompleteStringCollection = New AutoCompleteStringCollection()
    Dim ConnectionString As SqlConnection = New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;")
    Dim strSQL As String = "SELECT * from bill;"
    Dim SQLcommand As New SqlCommand(strSQL, ConnectionString)
    ConnectionString.Open()
    Dim reader As SqlDataReader
    reader = SQLcommand.ExecuteReader()

    While reader.Read()
        str.Add(reader.Item(1))
    End While

    Return str

End Function

This is the extra code I added before it stopped working, but I think this doesn't make any difference

Private Sub DataGridView2_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView2.RowPrePaint

    If e.RowIndex >= 0 Then
        Me.DataGridView2.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
    End If
End Sub
laik7k3q

laik7k3q1#

I found the solution. I tried changing every property of the datagridview randomly. And finally got to the point. It is the WRAP property of the column itself.

qgelzfjb

qgelzfjb2#

Wow, I had the problem of the autocomplete not working, and yes the Word Wrap stopped the autocomplete from working. Thanks Shreekant, sorry it took you ages to find out, but it sure helped me.

Also to do it for just one column, in the EditingControlShowing procedure, I used:

If the_DataGridView.Columns(the_DataGridView.CurrentCell.ColumnIndex).HeaderText = "the header text"

So that it only applies to that column. So many web pages and YouTube videos check for column(1) which is pure nonsense.

相关问题