如何绑定DataGridViewComboBoxColumn

时间:2016-08-25 14:28:16

标签: vb.net forms winforms datagridview datagridviewcombobox

我想使用vb.net填充DataGridViewComboBoxColumn字符串列表。 DataGridView包含其他列,这些列也非常重要。

1 个答案:

答案 0 :(得分:0)

也许这可能有助于你

''' <summary>
''' This example uses no backend data source.
''' 
''' Two controls, DataGridView named DataGridView1
''' and one Label named Label1
''' 
''' DataGridView has two columns created in the IDE
''' Column1 - DataGridViewComboBoxColumn
'''           * DisplayStyle = DataGridViewComboBoxDisplayStyle.
'''             Nothing set in the DataGridView Columns editor
''' Column2 - DataGridViewTextBoxColumn, nothing changed in settings
''' 
''' </summary>
''' <remarks></remarks>
Public Class Form2
    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        CType(DataGridView1.Columns("Column1"), DataGridViewComboBoxColumn).DataSource = _
            New String() {"Add", "Subtract", "Divide", "Multiply", "Total"}

        DataGridView1.Rows.Add(New Object() {"Divide", "B1"})
        DataGridView1.Rows.Add(New Object() {"Add", "B2"})
        DataGridView1.Rows.Add(New Object() {"Subtract", "B3"})
        DataGridView1.Rows.Add(New Object() {"Add", "B4"})
        DataGridView1.Rows.Add(New Object() {"Multiply", "B5"})
    End Sub
    ''' <summary>
    ''' If the event is triggered by the DataGridViewComboBoxColumn cast
    ''' e.Control to a ComboBox and setup SelectionChangedCommitted event
    ''' so that when the current cell value changes we can get it.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_EditingControlShowing( _
        ByVal sender As Object, _
        ByVal e As DataGridViewEditingControlShowingEventArgs) _
    Handles DataGridView1.EditingControlShowing

        If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Column1" Then
            Dim cb As ComboBox = TryCast(e.Control, ComboBox)
            RemoveHandler cb.SelectionChangeCommitted, AddressOf _SelectionChangeCommitted
            AddHandler cb.SelectionChangeCommitted, AddressOf _SelectionChangeCommitted
        End If

    End Sub
    Private Sub _SelectionChangeCommitted(sender As Object, e As EventArgs)
        Label1.Text = CType(sender, DataGridViewComboBoxEditingControl).Text
    End Sub
    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        If DataGridView1.Columns(e.ColumnIndex).Name = "Column1" Then
            If IsComboBoxCell(DataGridView1(e.ColumnIndex, e.RowIndex)) Then
                SendKeys.Send("{F4}")
            End If
        End If
    End Sub
    Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        AddHandler DataGridView1.CellEnter, AddressOf DataGridView1_CellEnter
    End Sub
    Public Function IsComboBoxCell(ByVal sender As DataGridViewCell) As Boolean
        If sender.EditType IsNot Nothing Then
            Return (sender.EditType Is GetType(DataGridViewComboBoxEditingControl))
        Else
            Return False
        End If
    End Function
End Class