[英]Datagridview DatagridviewComboboxColumn Editing DataBound
我有一個帶有datagridviewcomboboxcolumn的datagridview,在其上啟用了編輯。 只要沒有為列設置datapropertyname,此方法就起作用。
當設置了datapropertyname且我在組合框中鍵入內容時,系統會建議該項目,但按ENTER鍵時,將再次選擇先前選擇的項目。
當在按回車鍵后未設置數據屬性名稱時,將選擇建議的項目。
我的啟用編輯的代碼:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If e.Control.GetType Is GetType(DataGridViewComboBoxEditingControl) Then
Dim cb As ComboBox = TryCast(e.Control, ComboBox)
If cb IsNot Nothing Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
End Sub
Private Sub grdReceiving_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdReceiving.EditingControlShowing
If e.Control.GetType Is GetType(DataGridViewComboBoxEditingControl) Then
Dim cb As ComboBox = TryCast(e.Control, ComboBox)
If cb IsNot Nothing Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
End Sub
Private Sub grdReceiving_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles grdReceiving.CellValidating
Select Case e.ColumnIndex
Case 4
Dim comboBoxColumn As DataGridViewComboBoxColumn = grdReceiving.Columns(4)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
End If
End If
grdReceiving.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
Case 5
Dim comboBoxColumn As DataGridViewComboBoxColumn = grdReceiving.Columns(5)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
End If
End If
grdReceiving.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
Case 6
Dim comboBoxColumn As DataGridViewComboBoxColumn = grdReceiving.Columns(6)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
End If
End If
grdReceiving.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
End Select
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.