本实例利用 Paint方法添加一个下拉框到DataGrid1上
1、新建一个Visual Basic Project 。
2、添加一个DataGrid control到窗体上。
3、加入以下代码
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Region " Windows 窗体设计器生成的代码 "
Public Sub New ()
MyBase .New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose( ByVal disposing As Boolean )
If disposing Then
If Not (components Is Nothing ) Then
components.Dispose()
End If
End If
MyBase .Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2
3Me .DataGrid1 = New System.Windows.Forms.DataGrid()
4
5CType ( Me .DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
6
7Me .SuspendLayout()
8
9'
10
11'DataGrid1
12
13'
14
15Me .DataGrid1.DataMember = ""
16
17Me .DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
18
19Me .DataGrid1.Location = New System.Drawing.Point(72, 56)
20
21Me .DataGrid1.Name = "DataGrid1"
22
23Me .DataGrid1.Size = New System.Drawing.Size(416, 184)
24
25Me .DataGrid1.TabIndex = 0
26
27'
28
29'Form1
30
31'
32
33Me .AutoScaleBaseSize = New System.Drawing.Size(6, 14)
34
35Me .ClientSize = New System.Drawing.Size(552, 285)
36
37Me .Controls.AddRange( New System.Windows.Forms.Control() { Me .DataGrid1})
38
39Me .Name = "Form1"
40
41Me .Text = "Form1"
42
43CType ( Me .DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
44
45Me .ResumeLayout( False )
46
47End Sub
48
49# End Region
50
51Public MyCombo As New ComboBox()
52
53Dim con As New SqlConnection("server=lihg;uid=sa;pwd=sa;database=northwind")
54
55Dim daEmp As New SqlDataAdapter("Select * From Employees", con)
56
57Public ds As New DataSet()
58
59Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load
60
61AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged
62
63'Fill ComboBox list.
64
65MyCombo.Name = "MyCombo"
66
67MyCombo.Visible = False
68
69MyCombo.Items.Clear()
70
71MyCombo.Items.Add("Sales Representative")
72
73MyCombo.Items.Add("Inside Sales Coordinator")
74
75MyCombo.Items.Add("Vice President, Sales")
76
77MyCombo.Items.Add("Sales Manager")
78
79MyCombo.Items.Add("Flunky")
80
81daEmp.Fill(ds, "Employees")
82
83'Set the RowHeight of the DataGrid to the height of the ComboBox.
84
85DataGrid1.PreferredRowHeight = MyCombo.Height
86
87DataGrid1.DataSource = ds
88
89DataGrid1.DataMember = "Employees"
90
91'Add ComboBox to the Control collection of the DataGrid.
92
93DataGrid1.Controls.Add(MyCombo)
94
95End Sub
96
97Private Sub DataGrid1_Paint( ByVal sender As Object , ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
98
99If DataGrid1.CurrentCell.ColumnNumber = 3 Then
100
101MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width
102
103End If
104
105End Sub
106
107Private Sub Ctrls_TextChanged( ByVal sender As Object , ByVal e As System.EventArgs)
108
109If DataGrid1.CurrentCell.ColumnNumber = 3 Then
110
111MyCombo.Visible = False
112
113If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then
114
115SendKeys.Send("*")
116
117End If
118
119DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text
120
121End If
122
123End Sub
124
125Private Sub DataGrid1_CurrentCellChanged( ByVal sender As Object , ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
126
127If DataGrid1.CurrentCell.ColumnNumber = 3 Then
128
129MyCombo.Visible = False
130
131MyCombo.Width = 0
132
133MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left
134
135MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top
136
137MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""
138
139MyCombo.Visible = True
140
141Else
142
143MyCombo.Visible = False
144
145MyCombo.Width = 0
146
147End If
148
149End Sub
150
151Private Sub DataGrid1_Scroll( ByVal sender As Object , ByVal e As System.EventArgs) Handles DataGrid1.Scroll
152
153MyCombo.Visible = False
154
155MyCombo.Width = 0
156
157End Sub
158
159Private Sub DataGrid1_Click( ByVal sender As Object , ByVal e As System.EventArgs) Handles DataGrid1.Click
160
161MyCombo.Visible = False
162
163MyCombo.Width = 0
164
165End Sub
166
167End Class
168
1694、修连接字符串Dim con As New SqlConnection("server=lihg;uid=sa;pwd=sa;database=northwind"),使能连接上数据库
1705、F5运行</system.diagnostics.debuggerstepthrough()>