我这段代码是实现 DataGridVIew控件单元格显示ComboBox的vb程序,不知道对LZ来说有没有参考价值。 Public Class Form1 Private DataGridView1 As New DataGridView Private ComboBox1 As New ComboBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler DataGridView1.CellClick, AddressOf DataGridView1_CellClick AddHandler ComboBox1.TextChanged, AddressOf ComboBox1_TextChanged Me.DataGridView1.AllowUserToResizeRows = False Me.DataGridView1.AllowUserToResizeColumns = False Me.DataGridView1.Dock = DockStyle.Fill Me.DataGridView1.Columns.Add("Column1", "Column1") Me.DataGridView1.Columns.Add("Column2", "Column2") For i As Integer = 1 To 3 Me.DataGridView1.Rows.Add("Row" + i.ToString + " Column1", "Row" + i.ToString + " Column2") Next For i As Integer = 0 To 10 Me.ComboBox1.Items.Add("Item" + i.ToString) Next Me.ComboBox1.Visible = False Me.Controls.Add(ComboBox1) Me.Controls.Add(DataGridView1) End Sub Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) If e.ColumnIndex = 1 Then Dim p As Point = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location p.Offset(Me.DataGridView1.Left, Me.DataGridView1.Top) Me.ComboBox1.Location = p Me.ComboBox1.Size = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Size Me.ComboBox1.Visible = True Else Me.ComboBox1.Visible = False End If End Sub Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Me.DataGridView1.CurrentCell.Value = Me.ComboBox1.Text Me.ComboBox1.Visible = False End SubEnd Class
Public Class Form1 Private DataGridView1 As New DataGridView
Private ComboBox1 As New ComboBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.CellClick, AddressOf DataGridView1_CellClick
AddHandler ComboBox1.TextChanged, AddressOf ComboBox1_TextChanged Me.DataGridView1.AllowUserToResizeRows = False
Me.DataGridView1.AllowUserToResizeColumns = False
Me.DataGridView1.Dock = DockStyle.Fill Me.DataGridView1.Columns.Add("Column1", "Column1")
Me.DataGridView1.Columns.Add("Column2", "Column2") For i As Integer = 1 To 3
Me.DataGridView1.Rows.Add("Row" + i.ToString + " Column1", "Row" + i.ToString + " Column2")
Next For i As Integer = 0 To 10
Me.ComboBox1.Items.Add("Item" + i.ToString)
Next
Me.ComboBox1.Visible = False Me.Controls.Add(ComboBox1)
Me.Controls.Add(DataGridView1)
End Sub Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
If e.ColumnIndex = 1 Then
Dim p As Point = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
p.Offset(Me.DataGridView1.Left, Me.DataGridView1.Top)
Me.ComboBox1.Location = p
Me.ComboBox1.Size = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Size
Me.ComboBox1.Visible = True
Else
Me.ComboBox1.Visible = False
End If
End Sub Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Me.DataGridView1.CurrentCell.Value = Me.ComboBox1.Text
Me.ComboBox1.Visible = False
End SubEnd Class