2011-05-18 90 views
1

我有一個datagridview,並在其上啓用了編輯的datagridviewcombobox列。只要沒有爲該列設置datapropertyname,它就會工作。Datagridview DatagridviewComboboxColumn編輯DataBound

當datapropertyname被設置,我在組合框中輸入時,它應該按照建議輸入,但是當按下ENTER時,再次選擇先前選擇的項目。

當按下enter後未設置datapropertyname時,選擇了建議的項目。

我的代碼,使編輯:

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 
+0

你要爲你的更新模式,以OnValidation的數據源?我總是將我的ComboBoxStyle設置爲簡單的編輯或添加,如果它留在DropDownList上,您將在某個時刻出現索引問題。 – Bit 2011-05-18 11:12:40

+0

這就是爲什麼它設置爲DropDown,我仍然需要如此簡單的下拉按鈕將意味着一個文本框。只是在組合框中輸入時,從組合框中的項目中建議值,但在輸入時按下時沒有選中。 – 2011-05-18 11:20:09

+0

我通常選擇要編輯的項目,然後將組合框設置爲簡單編輯,然後保存更新的數據集。 – Bit 2011-05-18 12:27:12

回答

0
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 
+1

歡迎來到Stackoverflow!感謝您花時間回答這個問題。您可能想要回答這個鏈接http://stackoverflow.com/help/how-to-answer回答問題。你應該嘗試擴大你的答案,以包含更多的不僅僅是代碼。 – 2016-01-11 11:40:05