Ravi Ravi - 7 months ago 47
Vb.net Question

Auto-complete text in the datagridview for single column only

I tried to provide auto-complete text in datagridview. But, I got auto-complete text in all column and after clicking on third column (with combobox) application start displaying error on editing other cells.

I want auto-complete text box in first column i.e. "Name" only. I cannot manage code so that auto-complete doesn't show in second column i.e. "Age" and also error not occurred after clicking on combobox column.

The code is As follow.

Public Class Form1

Private Sub appData(ByVal data As AutoCompleteStringCollection, ByVal c As String)
data.Add("Ravi")
data.Add("Raj")
data.Add("Raja")
data.Add("r " & c)
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Try
MsgBox(e.Control.ToString)
Dim header As String = DataGridView1.Columns(0).HeaderText
If TypeOf e.Control Is TextBox AndAlso header.Equals("Name") AndAlso DataGridView1.CurrentCell.ColumnIndex = 0 Then
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim text As TextBox = TryCast(e.Control, TextBox)
If text IsNot Nothing Then
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource

Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data
End If
Else
Dim text As TextBox = TryCast(e.Control, TextBox)
text.AutoCompleteCustomSource = Nothing
text.AutoCompleteSource = AutoCompleteSource.None
text.AutoCompleteMode = AutoCompleteMode.None
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("ColName", "Name")
DataGridView1.Columns.Add("ColAge", "Age")
Dim ComCol As New DataGridViewComboBoxColumn
ComCol.Items.Add(1)
ComCol.Items.Add(2)
ComCol.HeaderText = "Combobox Col"
DataGridView1.Columns.Add(ComCol)
End Sub
End Class

Answer

Do a test in Else clause as :

If header.Equals("Name") Then

                If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                    Dim text As TextBox = TryCast(e.Control, TextBox)
                    If text IsNot Nothing Then
                        text.AutoCompleteMode = AutoCompleteMode.Suggest
                        text.AutoCompleteSource = AutoCompleteSource.CustomSource

                        Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
                        appData(data, DataGridView1.CurrentCellAddress.ToString)
                        text.AutoCompleteCustomSource = data
                    End If
                ElseIf TypeOf e.Control Is TextBox Then
                    Dim text As TextBox = TryCast(e.Control, TextBox)
                    text.AutoCompleteCustomSource = Nothing
                    text.AutoCompleteSource = AutoCompleteSource.None
                    text.AutoCompleteMode = AutoCompleteMode.None
                End If
            End If

   Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        Try

        Catch ex As Exception

        End Try
    End Sub