以下是我的代码:
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
当我在datagridview的组合框控件中键入"n"时,它会显示自动完成功能中的所有值,然后选择一个并按tab键,它不会在该字段中显示选定的值。当第二次做同样的事情时,它会在那里显示正确的选择。如何在第一时间实现这一点?
一旦编辑控件根据指定的条件进行验证,就应该查看DataGridView的CommitEdit
方法。
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
在这种情况下,您可以忽略CommitEdit
方法返回的值,因为提供的示例只允许来自集合的值。所以,若值不存在,单元格将不会被填充。
建议读取
我在使用DataGridViewComboBoxColumn
时也经历过,它需要2-3次点击才能编辑单元格的值。我建议使用:
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
在某个起点。上面的代码本身详细说明了它的作用。