本實例利用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運行
From:http://tw.wingwit.com/Article/program/net/201311/13633.html