VB .Net DataGridView ComboBox - 如何引用尚未保存的显示文本?

时间:2018-06-01 13:37:02

标签: vb.net datagridview combobox

问题在于:

我有一个与空数据集绑定的datagridview(通常,除非上一个会话没有完成订单)。用户将jobNumber和deliveryDate键入前2列,然后如果他们有需要使用的自定义电子邮件地址,他们可以从组合框中选择它(填充以前在formLoad上使用过的地址)或输入新的电子邮件,如果它不存在。问题是如果输入的电子邮件与组合框列表中的电子邮件不匹配,则它会在选项卡上将其删除。

在提交删除之前,我尝试了很多不同的东西来访问显示的(用户输入的)值,但似乎无法找到方法。我的工作如下所示。我基本上是通过自定义处理程序将键击捕获到组合框中。然后我将它添加到组合框列表中,然后选择该值。工作正常,但可能会混淆,具体取决于您访问组合框的方式。

有关尝试捕获用户在提交删除之前输入的整个字符串的想法吗?

Private Sub dGrid1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dGrid1.EditingControlShowing
    If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
        RemoveHandler e.Control.KeyDown, AddressOf cBox_KeyDown

        CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown
        CType(e.Control, ComboBox).AutoCompleteSource = AutoCompleteSource.ListItems
        CType(e.Control, ComboBox).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest

        holdThis = ""
        AddHandler e.Control.KeyDown, AddressOf cBox_KeyDown
    End If
End Sub

Private Sub cBox_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyCode = Keys.Space Then
        e.SuppressKeyPress = True

        holdThis = holdThis.Substring(1)

        Dim cBoxCol As DataGridViewComboBoxColumn
        cBoxCol = dGrid1.Columns.Item(3)
        cBoxCol.Items.Add(holdThis)
        cBoxCol = dGrid1.Columns.Item(4)
        cBoxCol.Items.Add(holdThis)

        dGrid1.CurrentCell.Value = holdThis
        Dim iCol = dGrid1.CurrentCell.ColumnIndex
        Dim iRow = dGrid1.CurrentCell.RowIndex
        If iCol = dGrid1.Columns.Count - 1 Then
            If iRow < dGrid1.Rows.Count - 1 Then
                dGrid1.CurrentCell = dGrid1(0, iRow + 1)
            End If
        Else
            dGrid1.CurrentCell = dGrid1(iCol + 1, iRow)
        End If

        holdThis = ""
    Else
        If e.KeyCode = Keys.Back Then
            holdThis = Strings.Left(holdThis, holdThis.Length - 1)
        ElseIf e.KeyCode = Keys.ShiftKey And Keys.D2 Then
            holdThis = holdThis & "@"
        ElseIf e.KeyCode = Keys.OemPeriod Then
            holdThis = holdThis & "."
        Else
            If holdThis.Length > 1 Then
                If holdThis(holdThis.Length - 1) = "2" Then
                    holdThis = Strings.Left(holdThis, holdThis.Length - 1)
                End If
            End If

            holdThis = holdThis & Chr(e.KeyValue)
            End If
        End If
End Sub

1 个答案:

答案 0 :(得分:0)

我明白了!此外,它现在可以在标签上工作,而不是依赖于用户按空格...这里是允许您访问datagridview组合框中未提及值的代码。

Private Sub dGrid1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dGrid1.EditingControlShowing
    If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
        RemoveHandler e.Control.PreviewKeyDown, AddressOf cBox_PreviewKeyDown

        CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown
        CType(e.Control, ComboBox).AutoCompleteSource = AutoCompleteSource.ListItems
        CType(e.Control, ComboBox).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append

        AddHandler e.Control.PreviewKeyDown, AddressOf cBox_PreviewKeyDown
    End If
End Sub

Private Sub cBox_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
    Dim cBox As DataGridViewComboBoxEditingControl = sender
    Dim val As Object = cBox.Text

    If e.KeyCode = Keys.Tab Then
        Dim cBoxCol As DataGridViewComboBoxColumn

        'SO E-Mail Col
        cBoxCol = dGrid1.Columns.Item(3)
        cBoxCol.Items.Add(val)
        'ADC E-Mail Col
        cBoxCol = dGrid1.Columns.Item(4)
        cBoxCol.Items.Add(val)

        'Set cBox Value
        dGrid1.CurrentCell.Value = val
    End If
End Sub