将值添加到DataGridviewComboBoxColumn而不会失去焦点

时间:2014-06-05 16:35:59

标签: vb.net visual-studio-2010 datagridview

我的表单中有一个DataGridView,它有一个ComboBoxColumn,它包含一个硬编码的植物列表。如果选择“其他”,我还会为用户提供添加其他工厂的选项。

Initial datagridview

当用户选择“其他”时,他将新的草名称放在消息框中。

Adding new name

但是,单击“确定”后,新名称不会添加到组合框中。 (Plant Spacing的值以编程方式添加)

After clicking OK

只有点击表格中的另一个单元格才会添加新名称。

enter image description here

如何在不丢失焦点的情况下更新组合框?

这是我点击Combobox时使用的代码

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    CB = TryCast(e.Control, System.Windows.Forms.ComboBox)
    If CB IsNot Nothing Then
        RemoveHandler CB.SelectedIndexChanged, AddressOf DGVComboIndexChanged
        AddHandler CB.SelectedIndexChanged, AddressOf DGVComboIndexChanged
    End If

    ' Other event handlers removed and added here

End Sub

Private Sub DGVComboIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    ' Do what you like with the current ComboBoxCell.
    'System.Windows.Forms.MessageBox.Show(String.Format( _
    '"The SelectedIndex was changed in ComboBoxCell: {0}" & _
    'Environment.NewLine & _
    '"The current item is: {1}", _
    'Me.DataGridView1.CurrentCellAddress.ToString(), _
    'CB.SelectedItem.ToString()))

    Dim TryAgain As Boolean = True
    Dim Letters As String = "abcdefghijklmnopqrstuvwxyz1234567890"
    Dim ComboColumn As System.Windows.Forms.DataGridViewComboBoxColumn

    ComboColumn = DataGridView1.Columns(0)
    Try
        If CB.SelectedItem.ToString = "Other" Then
            While TryAgain
                OtherGrass = Microsoft.VisualBasic.InputBox("Enter the alternate plant name", "Other plant", "")
                If OtherGrass = "" Then
                    'return the cell to ""
                    DataGridView1.CurrentRow.Cells(0).Value = ""
                    Exit Sub
                End If
                For i As Integer = 1 To Len(Letters)
                    If InStr(LCase(OtherGrass), Mid(Letters, i, 1)) > 0 Then
                        TryAgain = False
                        Exit For
                    End If
                Next
                For i As Integer = 0 To ComboColumn.Items.Count - 1
                    If LCase(OtherGrass) = LCase(ComboColumn.Items.Item(i).ToString) Then
                        TryAgain = True
                        System.Windows.Forms.MessageBox.Show("This plant has already been added.")
                    End If
                Next
            End While
            'ReDim Preserve Grasses(Grasses.GetUpperBound(0) + 1)
            'Grasses(Grasses.GetUpperBound(0)) = OtherGrass
            ComboColumn.Items.Add(OtherGrass)
        End If

        If DataGridView1.CurrentRow.Cells(1).Value Is Nothing Then
            DataGridView1.CurrentRow.Cells(1).Value = txtSpacMult.Text
        End If
        'If CB.SelectedItem.ToString <> "" Then
        '  For i As Integer = 1 To DataGridView1.Columns.Count - 2
        '    Dim temp As Boolean = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly
        '    Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly = False
        '    temp = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(i).ReadOnly
        '  Next
        'End If

        EnableRun()

    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

而不是:

            ComboColumn.Items.Add(OtherGrass)

把:

            CB.Items.RemoveAt(CB.Items.Count - 1)
            CB.Items.Add(OtherGrass)
            CB.Items.Add("Other")
            CB.SelectedItem = OtherGrass

您应该添加和选择新工厂,以及&#34;其他&#34;进入结束。