简体   繁体   English

DataGrid中的vb.net combox框

[英]vb.net combox box within a datagrid

I have a combobox with a datagrid, but I'd like the user to be able to type into the combobox like usual; 我有一个带有数据网格的组合框,但是我希望用户能够像往常一样键入组合框; At the minute it's a fixed dropdown. 在一分钟内它是固定的下拉列表。 Here is my code: 这是我的代码:

  Dim NewColumn As New DataGridViewComboBoxColumn() 'Declare new DGV CC

    With NewColumn 'Set Properties
        .DataPropertyName = "NewColumn" 'Name
        .HeaderText = "New Column" 'Heading
        .DropDownWidth = 160 'Width Of DropDown Box
        .Width = 90 'Display Width
        '.MaxDropDownItems = 5 'How Many Items To Drop Down At A Time
        .FlatStyle = FlatStyle.Flat 'Appearance
        .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox

        .Items.Add("Screw Fix 1") 'Add Some Text Items
        .Items.Add("Fix 1")
        .Items.Add("3 Stone")
        .Items.Add("34 Stone")
        .Items.Add("5")
        .Items.Add("6")
        .Items.Add("7")
        .Items.Add("8")
        .Items.Add("9")
        .Items.Add("10")
    End With
    dgDetails.Columns.Add(NewColumn) 'Add The Column

There isn't natively you need to handle two events 您本来就不需要处理两个事件

The first to allow the user to type in the new value 第一个允许用户输入新值的

Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dataGridView1.EditingControlShowing
    Dim c As ComboBox = TryCast(e.Control, ComboBox)
    If c IsNot Nothing Then
        c.DropDownStyle = ComboBoxStyle.DropDown
    End If
End Sub

The second to actually insert the new value 第二个实际插入新值

Private Sub dataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handels dataGridView1.CellValidating
    Dim comboBoxColumn As DataGridView.Column = dataGridView1.Columns("yourColumnName")
    If e.ColumnIndex = comboBoxColumn.Index Then
        Dim eFV As Object = e.FormattedValue
        If Not comboBoxColumn.Items.Contains(eFV) Then
            comboBoxColumn.Items.Add(eFV)
                    comboBoxColumn.SelectedIndex = ComboBox.Items.Count - 1
        End If
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM