借助ComboBox实现DataGridView列下拉选择效果

窗体上放置一个DataGridView控件,一个ComboBox控件,代码如下:
情况一、DataGridView的数据手工建立:

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With DataGridView1
'只读,不允许修改单元格内容
.ReadOnly = True
'禁止多选单元格
.MultiSelect = False
'设定行数与列数
.RowCount = 10
.ColumnCount = 4
'添加DataGridView标题
For i As Integer = 0 To .ColumnCount - 1
.Columns(i).HeaderText = "第" & i + 1 & "列"
Next
'设置ComboBox样式
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
'为ComboBox添加项(用数组)
ComboBox1.Items.AddRange({"A", "B", "C"})
'设置ComboBox的父容器为DataGridView,这样ComboBox的坐标就是相对DataGridView,而不是Form
ComboBox1.Parent = DataGridView1
'先不显示ComboBox
ComboBox1.Visible = False
End With
End Sub

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
With DataGridView1
'获取选定单元格的坐标:X、Y
If .SelectedCells.Count <> 0 Then
Dim cellX As Integer = .GetCellDisplayRectangle(.SelectedCells(0).ColumnIndex, .SelectedCells(0).RowIndex, False).X
Dim cellY As Integer = .GetCellDisplayRectangle(.SelectedCells(0).ColumnIndex, .SelectedCells(0).RowIndex, False).Y
'设置ComboBox的Size为选定单元格的Size
ComboBox1.Size = .SelectedCells(0).Size
'设置ComboBox的坐标,以便覆盖选定的单元格,达到单元格下拉效果
ComboBox1.Left = cellX
ComboBox1.Top = cellY
If DataGridView1.SelectedCells(0).Value <> vbNullString Then
ComboBox1.Text = DataGridView1.SelectedCells(0).Value
End If
End If
End With
End Sub

Private Sub ComboBox1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.LocationChanged
'选定单元格开始触发
If DataGridView1.SelectedCells.Count <> 0 Then
'判断哪一列显示ComboBox下拉
If DataGridView1.SelectedCells(0).ColumnIndex = 1 Then
ComboBox1.Visible = True
'如果DataGridView单元格已经有数据,则让Combo的值自动显示其数据值,否则就显示空
If DataGridView1.SelectedCells(0).Value <> vbNullString Then
ComboBox1.Text = DataGridView1.SelectedCells(0).Value
Else
ComboBox1.SelectedIndex = -1
End If
Else
ComboBox1.Visible = False
End If
End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'将Combo的内容写入DataGridview选定的单元格
DataGridView1.SelectedCells(0).Value = ComboBox1.Text
End Sub

Private Sub DataGridView1_ColumnWidthChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
'在允许调整DataGridView列宽的情况下,DataGridView调整列宽时,让ComboBox宽度随之改变
If e.Column.Index = 1 Then '限定是哪一列调整列宽时才触发
ComboBox1.Width = e.Column.Width
End If
End Sub
End Class


情况二、DataGridView的数据来源于DataTable:
主要演示如何手工创建DataTable,来源数据库的话比较简单,此处不演示

Imports System.Data
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With DataGridView1
'只读,不允许修改单元格内容
.ReadOnly = True
'禁止多选单元格
.MultiSelect = False
'禁止删除和增加行及调整行高
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeRows = False

'创建DataTable()
Dim dt As DataTable = New DataTable
'为DataTable添加列
dt.Columns.Add(New DataColumn("id", System.Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Name", System.Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("Level", System.Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("Memo", System.Type.GetType("System.String")))
'为DataTable添加行
'方法一添加新行:
Dim dr As DataRow
dr = dt.NewRow
dr("id") = 1
dr("Name") = "张三"
dt.Rows.Add(dr)
dr = dt.NewRow
'方法二添加新行
dt.Rows.Add(New Object() {"2", "李四"})
dt.Rows.Add(New Object() {"3", "王五"})

'设置DataGridView数据源为DataTable
.DataSource = dt

'设置ComboBox样式
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
'为ComboBox添加项(用数组)
ComboBox1.Items.AddRange({"A", "B", "C"})
'设置ComboBox的父容器为DataGridView,这样ComboBox的坐标就是相对DataGridView,而不是Form
ComboBox1.Parent = DataGridView1
'先不显示ComboBox
ComboBox1.Visible = False
End With
End Sub
Private Sub DataGridView1_ColumnWidthChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
'在允许调整DataGridView列宽的情况下,DataGridView调整列宽时,让ComboBox宽度随之改变
If e.Column.Index = 2 Then '限定是哪一列调整列宽时才触发
ComboBox1.Width = e.Column.Width
End If
End Sub

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
With DataGridView1
'获取选定单元格的坐标:X、Y
If DataGridView1.SelectedCells.Count <> 0 Then
Dim cellX As Integer = .GetCellDisplayRectangle(.SelectedCells(0).ColumnIndex, .SelectedCells(0).RowIndex, False).X
Dim cellY As Integer = .GetCellDisplayRectangle(.SelectedCells(0).ColumnIndex, .SelectedCells(0).RowIndex, False).Y
'设置ComboBox的Size为选定单元格的Size
ComboBox1.Size = .SelectedCells(0).Size
'设置ComboBox的坐标,以便覆盖选定的单元格,达到单元格下拉效果
ComboBox1.Left = cellX
ComboBox1.Top = cellY
If DataGridView1.SelectedCells(0).Value.ToString <> vbNullString Then
ComboBox1.Text = DataGridView1.SelectedCells(0).Value
End If
End If
End With
End Sub

Private Sub ComboBox1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.LocationChanged
'选定单元格开始触发
If DataGridView1.SelectedCells.Count <> 0 Then
'判断哪一列显示ComboBox下拉
If DataGridView1.SelectedCells(0).ColumnIndex = 2 Then
ComboBox1.Visible = True
'如果DataGridView单元格已经有数据,则让Combo的值自动显示其数据值,否则就显示空
If DataGridView1.SelectedCells(0).Value.ToString <> vbNullString Then
ComboBox1.Text = DataGridView1.SelectedCells(0).Value
Else
ComboBox1.SelectedIndex = -1
End If
Else
ComboBox1.Visible = False
End If
End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'将Combo的内容写入DataGridview选定的单元格
DataGridView1.SelectedCells(0).Value = ComboBox1.Text
End Sub
End Class




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值