Datagrid 中添加ComboBox

Datagrid的列支持两种样式,分别是DatagridTextColumn和DatagridBoolColumn,前者表现为单元格时为Textbox ,
后者表现为单元格时为一个CheckBox.
要在Datagrid 中添加ComboBox,擦采用如下方法:
  Dim MyCombo As New ComboBox
    
添加到Datagrid格式化中
 AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged
        '设置MyCombo的Name以及Visible属性
        MyCombo.Name = "MyCombo"
        MyCombo.Visible = False
        MyCombo.DropDownStyle = ComboBoxStyle.DropDown
        '清空MyCombo
        MyCombo.Items.Clear()
        '给MyCombo添加项
       Me.MyCombo.Items.Add(" ")

        '把MyCombo加入到dgdGoodInfo的Controls集合中
        dgdGoodInfo.Controls.Add(MyCombo)

添加函数,MyCombo 添加到第四列
Private Sub Ctrls_TextChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs)
        '判断DataGrid的当前单元格是否属于第四列
        'DataGrid的列是从0开始
        If dgdInfo.CurrentCell.ColumnNumber = 4 Then
            If dgdInfo.Item(dgdInfo.CurrentCell) & "" = "" Then
                SendKeys.Send(" ")
            End If
            '设置当前单元格的值为MyCombo选中的项的Text
            dgdInfo.Item(dgdInfo.CurrentCell) = MyCombo.Text
        End If
    End Sub

    Private Sub dgdInfo_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles dgdInfo.Click
        '设置MyCombo的Visible,Width属性
        MyCombo.Visible = False
        MyCombo.Width = 0
    End Sub

    Private Sub dgdInfo_CurrentCellChanged(ByVal sender _
    As Object, ByVal e As System.EventArgs) Handles _
    dgdInfo.CurrentCellChanged
        If dgdInfo.CurrentCell.ColumnNumber = 4 Then
            MyCombo.Visible = False
            MyCombo.Width = 0
            MyCombo.Left = dgdInfo.GetCurrentCellBounds.Left
            MyCombo.Top = dgdInfo.GetCurrentCellBounds.Top
            MyCombo.Text = _
            dgdGoodInfo.Item(dgdInfo.CurrentCell) & ""
            MyCombo.Visible = True
        Else
            MyCombo.Visible = False
            MyCombo.Width = 0
        End If
    End Sub

    Private Sub dgdInfo_Paint(ByVal sender As Object, _
    ByVal e As PaintEventArgs) Handles dgdGoodInfo.Paint
        If dgdInfo.CurrentCell.ColumnNumber = 4 Then
            MyCombo.Width = dgdInfo.GetCurrentCellBounds.Width
        End If
    End Sub

    Private Sub dgdInfo_Scroll(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles dgdInfo.Scroll
        MyCombo.Visible = False
        MyCombo.Width = 0
    End Sub
注:dgdInfo 为DataGrid.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值