2

Following is my code:

    Public Class Form1

Private DT_LocalTransactionList As DataTable

Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
    DT_LocalTransactionList = New DataTable
    DT_LocalTransactionList.Columns.Add("TransactionName")
    DT_LocalTransactionList.Columns.Add("TransactionType")
    For iVisible As Integer = 0 To 5
        DT_LocalTransactionList.Rows.Add()
        DT_LocalTransactionList.Rows(iVisible).Item("TransactionName") = "Name " & iVisible
        DT_LocalTransactionList.Rows(iVisible).Item("TransactionType") = "Add " & iVisible
    Next
    If e.ColumnIndex = colName.Index Then
        Dim dgvCbo As New DataGridViewComboBoxCell
        dgvCbo = TryCast(DataGridView1(colName.Index, e.RowIndex), DataGridViewComboBoxCell)
        dgvCbo.DataSource = DT_LocalTransactionList
        dgvCbo.DisplayMember = "TransactionName"
        dgvCbo.ValueMember = "TransactionType"
    End If
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Select Case Me.DataGridView1.CurrentCell.ColumnIndex

        Case colName.Index
            If TypeOf e.Control Is ComboBox Then
                Dim cb As ComboBox = TryCast(e.Control, ComboBox)
                cb.DropDownStyle = ComboBoxStyle.DropDown
                cb.AutoCompleteSource = AutoCompleteSource.ListItems
                cb.AutoCompleteMode = AutoCompleteMode.Suggest
                RemoveHandler cb.DrawItem, AddressOf GridCombo_DrawItem
                RemoveHandler cb.DropDownClosed, AddressOf cbDropDownClosed
                RemoveHandler cb.Validating, AddressOf GridCombo_Validating
                RemoveHandler cb.KeyDown, AddressOf GridCombo_KeyDown
                AddHandler cb.DrawItem, AddressOf GridCombo_DrawItem
                cb.DrawMode = DrawMode.OwnerDrawFixed
                AddHandler cb.DropDownClosed, AddressOf cbDropDownClosed
                AddHandler cb.Validating, AddressOf GridCombo_Validating
                AddHandler cb.KeyDown, AddressOf GridCombo_KeyDown
            End If
    End Select
End Sub

Private Sub GridCombo_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
    Dim text As String = sender.GetItemText(sender.Items(e.Index))

    e.DrawBackground()

    Using br As New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds)
    End Using

    e.DrawFocusRectangle()
End Sub

Private Sub cbDropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Private Sub GridCombo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    Dim cb As ComboBox = TryCast(sender, ComboBox)

    If Not IsItemExistInList(cb) Then
        e.Cancel = True
    End If
End Sub

Private Sub GridCombo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    Dim cb As ComboBox = TryCast(sender, ComboBox)
    cb.Refresh()

    If e.KeyCode = Keys.Enter Then
        If IsItemExistInList(cb) Then
            System.Windows.Forms.SendKeys.Send("{TAB}")
        End If
    End If
End Sub

Public Function IsItemExistInList(ByRef cboCombo As ComboBox) As Boolean
    Dim blnContinue As Boolean
    Dim intCount As Integer

    blnContinue = False

    If cboCombo.Text.Trim = "" Then
        blnContinue = True
    End If

    If blnContinue = False Then
        For intCount = 0 To cboCombo.Items.Count - 1 And blnContinue = False
            If cboCombo.Text.Trim = cboCombo.GetItemText(cboCombo.Items(intCount)).Trim Then
                blnContinue = True
            End If
        Next
    End If

    IsItemExistInList = blnContinue
End Function

   End Class

When I type 'n' in the combobox control of datagridview then it shows all the values from Autocomplete feature, then selecting one and pressing tab it does not show the selected value in that field. When second time doing the same thing it shows correct selection there. How to implement that thing in first time?

Sanjoy Das
  • 21
  • 1
  • 4

1 Answers1

1

You should look at CommitEdit method of DataGridView once the editing control is validating as per the criteria specified.

Private Sub GridCombo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    Dim cb As ComboBox = TryCast(sender, ComboBox)

    If Not IsItemExistInList(cb) Then
        e.Cancel = True
    Else
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
    End If
End Sub

In this case you can ignore the value returned by CommitEdit method since the provided sample only allows values from the collection. So if value does not exist cell will not be populated.

Recommended read

I have also experienced while working with DataGridViewComboBoxColumn that it require 2-3 click in order to edit value of the cell. I would suggest to use:

DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter

at some starting point. Above code itself elaborates what it does.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56