本实例利用Paint方法添加一个下拉框到DataGrid上 office:office />
新建一个Visual Basic Project
添加一个DataGrid control到窗体上
加入以下代码
Imports SystemDataSqlClient
Public Class Form
Inherits SystemWindowsFormsForm
#Region Windows 窗体设计器生成的代码
Public Sub New()
MyBaseNew()
该调用是 Windows 窗体设计器所必需的
InitializeComponent()
在 InitializeComponent() 调用之后添加任何初始化
End Sub
窗体重写处置以清理组件列表
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
componentsDispose()
End If
End If
MyBaseDispose(disposing)
End Sub
Windows 窗体设计器所必需的
Private components As SystemComponentModelIContainer
注意以下过程是 Windows 窗体设计器所必需的
可以使用 Windows 窗体设计器修改此过程
不要使用代码编辑器修改它
Friend WithEvents DataGrid As SystemWindowsFormsDataGrid
<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
MeDataGrid = New SystemWindowsFormsDataGrid()
CType(MeDataGrid SystemComponentModelISupportInitialize)BeginInit()
MeSuspendLayout()
DataGrid
MeDataGridDataMember =
MeDataGridHeaderForeColor = SystemDrawingSystemColorsControlText
MeDataGridLocation = New SystemDrawingPoint( )
MeDataGridName = DataGrid
MeDataGridSize = New SystemDrawingSize( )
MeDataGridTabIndex =
Form
MeAutoScaleBaseSize = New SystemDrawingSize( )
MeClientSize = New SystemDrawingSize( )
MeControlsAddRange(New SystemWindowsFormsControl() {MeDataGrid})
MeName = Form
MeText = Form
CType(MeDataGrid SystemComponentModelISupportInitialize)EndInit()
MeResumeLayout(False)
End Sub
#End Region
Public MyCombo As New ComboBox()
Dim con As New SqlConnection(server=lihg;uid=sa;pwd=sa;database=northwind)
Dim daEmp As New SqlDataAdapter(Select * From Employees con)
Public ds As New DataSet()
Private Sub Form_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad
AddHandler MyComboTextChanged AddressOf Ctrls_TextChanged
Fill ComboBox list
MyComboName = MyCombo
MyComboVisible = False
MyComboItemsClear()
MyComboItemsAdd(Sales Representative)
MyComboItemsAdd(Inside Sales Coordinator)
MyComboItemsAdd(Vice President Sales)
MyComboItemsAdd(Sales Manager)
MyComboItemsAdd(Flunky)
daEmpFill(ds Employees)
Set the RowHeight of the DataGrid to the height of the ComboBox
DataGridPreferredRowHeight = MyComboHeight
DataGridDataSource = ds
DataGridDataMember = Employees
Add ComboBox to the Control collection of the DataGrid
DataGridControlsAdd(MyCombo)
End Sub
Private Sub DataGrid_Paint(ByVal sender As Object ByVal e As SystemWindowsFormsPaintEventArgs) Handles DataGridPaint
If DataGridCurrentCellColumnNumber = Then
MyComboWidth = DataGridGetCurrentCellBoundsWidth
End If
End Sub
Private Sub Ctrls_TextChanged(ByVal sender As Object ByVal e As SystemEventArgs)
If DataGridCurrentCellColumnNumber = Then
MyComboVisible = False
If DataGridItem(DataGridCurrentCell) & = Then
SendKeysSend(*)
End If
DataGridItem(DataGridCurrentCell) = MyComboText
End If
End Sub
Private Sub DataGrid_CurrentCellChanged(ByVal sender As Object ByVal e As SystemEventArgs) Handles DataGridCurrentCellChanged
If DataGridCurrentCellColumnNumber = Then
MyComboVisible = False
MyComboWidth =
MyComboLeft = DataGridGetCurrentCellBoundsLeft
MyComboTop = DataGridGetCurrentCellBoundsTop
MyComboText = DataGridItem(DataGridCurrentCell) &
MyComboVisible = True
Else
MyComboVisible = False
MyComboWidth =
End If
End Sub
Private Sub DataGrid_Scroll(ByVal sender As Object ByVal e As SystemEventArgs) Handles DataGridScroll
MyComboVisible = False
MyComboWidth =
End Sub
Private Sub DataGrid_Click(ByVal sender As Object ByVal e As SystemEventArgs) Handles DataGridClick
MyComboVisible = False
MyComboWidth =
End Sub
End Class
修连接字符串Dim con As New SqlConnection(server=lihg;uid=sa;pwd=sa;database=northwind)使能连接上数据库
F运行 |