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.