【问题标题】:How do I bind a DataGridViewComboBoxColumn如何绑定 DataGridViewComboBoxColumn
【发布时间】:2017-01-02 01:07:34
【问题描述】:

我想使用 vb.net 用字符串列表填充 DataGridViewComboBoxColumnDataGridView 包含其他对于显示也非常重要的列。

【问题讨论】:

    标签: vb.net forms winforms datagridview datagridviewcombobox


    【解决方案1】:

    也许这对你有帮助

    ''' <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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 2013-10-24
      相关资源
      最近更新 更多