最近在做一个程序,用DataGridView中自带的DataGridViewComboBoxColumn列动态生成下拉列表,真是问题多多
所有想在DataGridView列中嵌入ComboBox以实现动态生成下拉列表的目的
最后找到http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.html,但是C#版,我做的程序是VB.NET的
最后费了九牛之力,参照上面文章中源码,做成了VB.NET版的,我想有不少朋友需要,不惜放源出来,与大家一起学习
1 Imports System
2 Imports System.Data
3 Imports System.Collections.Generic
4 Imports System.Drawing
5 Imports System.Text
6
7 PublicClass Form1
8 Dim cmb_Temp AsNew ComboBox
9
10
11 PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
12 '绑定性别下拉列表框
13 BindSex()
14 '绑定数据表
15 BindData()
16 '设置下拉列表框不可见
17 cmb_Temp.Visible =False
18 '添加下拉列表框事件
19 AddHandler cmb_Temp.SelectedIndexChanged, AddressOf cmb_Temp_SelectedIndexChanged
20
21 '将下拉列表框加入到DataGridView控件
22 dgv_User.Controls.Add(cmb_Temp)
23 End Sub
24
25
26 '''<summary>
27 '''绑定性别下拉列表框
28 '''</summary>
29 Sub BindSex()
30 Dim dtSex As DataTable =New DataTable
31 dtSex.Columns.Add("Value")
32 dtSex.Columns.Add("Name")
33 Dim drSex As DataRow
34 drSex = dtSex.NewRow
35 drSex.Item(0) ="1"
36 drSex.Item(1) ="男"
37 dtSex.Rows.Add(drSex)
38 drSex = dtSex.NewRow()
39 drSex.Item(0) ="0"
40 drSex.Item(1) ="女"
41 dtSex.Rows.Add(drSex)
42 'cmb_Temp.ValueMember = "Value"
43 cmb_Temp.ValueMember ="Value"
44
45 cmb_Temp.DisplayMember ="Name"
46 cmb_Temp.DataSource = dtSex
47 cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList
48 End Sub
49
50 '''<summary>
51 ''' 创建DataGridView数据表
52 '''</summary>
53 Sub BindData()
54 Dim dtData As DataTable =New DataTable
55 dtData.Columns.Add("ID")
56 dtData.Columns.Add("Name")
57 dtData.Columns.Add("Sex")
58 Dim drData As DataRow
59
60 drData = dtData.NewRow()
61 drData.Item(0) =1
62 drData.Item(1) ="张三"
63 drData.Item(2) ="1"
64 dtData.Rows.Add(drData)
65 drData = dtData.NewRow()
66 drData.Item(0) =2
67 drData.Item(1) ="李四"
68 drData.Item(2) ="1"
69 dtData.Rows.Add(drData)
70 drData = dtData.NewRow()
71 drData.Item(0) =3
72 drData.Item(1) ="王五"
73 drData.Item(2) ="1"
74 dtData.Rows.Add(drData)
75 drData = dtData.NewRow()
76 drData.Item(0) =4
77 drData.Item(1) ="小芳"
78 drData.Item(2) ="0"
79 dtData.Rows.Add(drData)
80 drData = dtData.NewRow()
81 drData.Item(0) =5
82 drData.Item(1) ="小娟"
83 drData.Item(2) ="0"
84 dtData.Rows.Add(drData)
85 drData = dtData.NewRow()
86 drData.Item(0) =6
87 drData.Item(1) ="赵六"
88 drData.Item(2) ="1"
89 dtData.Rows.Add(drData)
90 dgv_User.DataSource = dtData
91 End Sub
92 PrivateSub dgv_User_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv_User.CurrentCellChanged
93 '当用户选择的单元格移动到性别这一列时,我们要显示下拉列表框
94 Try
95 If dgv_User.CurrentCell.ColumnIndex =2Then
96 '获取位置
97 Dim rect As Rectangle = dgv_User.GetCellDisplayRectangle(dgv_User.CurrentCell.ColumnIndex, dgv_User.CurrentCell.RowIndex, False)
98 Dim sexValue AsString= dgv_User.CurrentCell.Value.ToString()
99 If sexValue ="1"Then
100 cmb_Temp.Text ="男"
101 Else
102 cmb_Temp.Text ="女"
103 EndIf
104 '设置组合框位置
105 cmb_Temp.Location =New Point(rect.X, rect.Y)
106 cmb_Temp.Width = rect.Width
107 cmb_Temp.Height = rect.Height
108 cmb_Temp.Visible =True
109 Else
110 cmb_Temp.Visible =False
111 EndIf
112 Catch ex As Exception
113 EndTry
114 End Sub
115
116 '''<summary>
117 ''' 选择项目后,填充单元格内容
118 '''</summary>
119 Sub cmb_Temp_SelectedIndexChanged(ByVal sender AsObject, ByVal e As EventArgs)
120
121 IfTypeOf sender Is ComboBox Then
122 IfCType(sender, ComboBox).Text ="男"Then
123 dgv_User.CurrentCell.Value ="男"
124 dgv_User.CurrentCell.Tag ="1"
125
126 Else
127 dgv_User.CurrentCell.Value ="女"
128 dgv_User.CurrentCell.Tag ="0"
129 EndIf
130 EndIf
131 End Sub
132
133 '''<summary>
134 ''' 当滚动DataGridView或者改变DataGridView列宽时将下拉列表框设为不可见
135 '''</summary>
136 PrivateSub dgv_User_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgv_User.Scroll
137 cmb_Temp.Visible =False
138 End Sub
139 PrivateSub dgv_User_ColumnWidthChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles dgv_User.ColumnWidthChanged
140 cmb_Temp.Visible =False
141 End Sub
2 Imports System.Data
3 Imports System.Collections.Generic
4 Imports System.Drawing
5 Imports System.Text
6
7 PublicClass Form1
8 Dim cmb_Temp AsNew ComboBox
9
10
11 PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
12 '绑定性别下拉列表框
13 BindSex()
14 '绑定数据表
15 BindData()
16 '设置下拉列表框不可见
17 cmb_Temp.Visible =False
18 '添加下拉列表框事件
19 AddHandler cmb_Temp.SelectedIndexChanged, AddressOf cmb_Temp_SelectedIndexChanged
20
21 '将下拉列表框加入到DataGridView控件
22 dgv_User.Controls.Add(cmb_Temp)
23 End Sub
24
25
26 '''<summary>
27 '''绑定性别下拉列表框
28 '''</summary>
29 Sub BindSex()
30 Dim dtSex As DataTable =New DataTable
31 dtSex.Columns.Add("Value")
32 dtSex.Columns.Add("Name")
33 Dim drSex As DataRow
34 drSex = dtSex.NewRow
35 drSex.Item(0) ="1"
36 drSex.Item(1) ="男"
37 dtSex.Rows.Add(drSex)
38 drSex = dtSex.NewRow()
39 drSex.Item(0) ="0"
40 drSex.Item(1) ="女"
41 dtSex.Rows.Add(drSex)
42 'cmb_Temp.ValueMember = "Value"
43 cmb_Temp.ValueMember ="Value"
44
45 cmb_Temp.DisplayMember ="Name"
46 cmb_Temp.DataSource = dtSex
47 cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList
48 End Sub
49
50 '''<summary>
51 ''' 创建DataGridView数据表
52 '''</summary>
53 Sub BindData()
54 Dim dtData As DataTable =New DataTable
55 dtData.Columns.Add("ID")
56 dtData.Columns.Add("Name")
57 dtData.Columns.Add("Sex")
58 Dim drData As DataRow
59
60 drData = dtData.NewRow()
61 drData.Item(0) =1
62 drData.Item(1) ="张三"
63 drData.Item(2) ="1"
64 dtData.Rows.Add(drData)
65 drData = dtData.NewRow()
66 drData.Item(0) =2
67 drData.Item(1) ="李四"
68 drData.Item(2) ="1"
69 dtData.Rows.Add(drData)
70 drData = dtData.NewRow()
71 drData.Item(0) =3
72 drData.Item(1) ="王五"
73 drData.Item(2) ="1"
74 dtData.Rows.Add(drData)
75 drData = dtData.NewRow()
76 drData.Item(0) =4
77 drData.Item(1) ="小芳"
78 drData.Item(2) ="0"
79 dtData.Rows.Add(drData)
80 drData = dtData.NewRow()
81 drData.Item(0) =5
82 drData.Item(1) ="小娟"
83 drData.Item(2) ="0"
84 dtData.Rows.Add(drData)
85 drData = dtData.NewRow()
86 drData.Item(0) =6
87 drData.Item(1) ="赵六"
88 drData.Item(2) ="1"
89 dtData.Rows.Add(drData)
90 dgv_User.DataSource = dtData
91 End Sub
92 PrivateSub dgv_User_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv_User.CurrentCellChanged
93 '当用户选择的单元格移动到性别这一列时,我们要显示下拉列表框
94 Try
95 If dgv_User.CurrentCell.ColumnIndex =2Then
96 '获取位置
97 Dim rect As Rectangle = dgv_User.GetCellDisplayRectangle(dgv_User.CurrentCell.ColumnIndex, dgv_User.CurrentCell.RowIndex, False)
98 Dim sexValue AsString= dgv_User.CurrentCell.Value.ToString()
99 If sexValue ="1"Then
100 cmb_Temp.Text ="男"
101 Else
102 cmb_Temp.Text ="女"
103 EndIf
104 '设置组合框位置
105 cmb_Temp.Location =New Point(rect.X, rect.Y)
106 cmb_Temp.Width = rect.Width
107 cmb_Temp.Height = rect.Height
108 cmb_Temp.Visible =True
109 Else
110 cmb_Temp.Visible =False
111 EndIf
112 Catch ex As Exception
113 EndTry
114 End Sub
115
116 '''<summary>
117 ''' 选择项目后,填充单元格内容
118 '''</summary>
119 Sub cmb_Temp_SelectedIndexChanged(ByVal sender AsObject, ByVal e As EventArgs)
120
121 IfTypeOf sender Is ComboBox Then
122 IfCType(sender, ComboBox).Text ="男"Then
123 dgv_User.CurrentCell.Value ="男"
124 dgv_User.CurrentCell.Tag ="1"
125
126 Else
127 dgv_User.CurrentCell.Value ="女"
128 dgv_User.CurrentCell.Tag ="0"
129 EndIf
130 EndIf
131 End Sub
132
133 '''<summary>
134 ''' 当滚动DataGridView或者改变DataGridView列宽时将下拉列表框设为不可见
135 '''</summary>
136 PrivateSub dgv_User_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgv_User.Scroll
137 cmb_Temp.Visible =False
138 End Sub
139 PrivateSub dgv_User_ColumnWidthChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles dgv_User.ColumnWidthChanged
140 cmb_Temp.Visible =False
141 End Sub