2
以下是我的代碼:組合框在vb.net datagridview的未示出的值第一次
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」然後將其示出了來自自動填充功能的所有的值,然後選擇一個和按下標籤它不會顯示該字段中的選定值。當第二次做同樣的事情時,它顯示正確的選擇。如何在第一時間執行該事情?