65.9K
CodeProject 正在变化。 阅读更多。
Home

用于在VB6中使用DataGrid的ActiveX控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (14投票s)

2008年3月9日

CPOL

3分钟阅读

viewsIcon

258238

downloadIcon

17936

用于使用MSDataGrid以及ComboBox和DTPicker的ActiveX。

Eng01.JPG

引言

我之前写过一篇关于使用 (MSFlexGrid) 编辑单元格的文章,并在 FlexGrid 的任何列中包含 (ComboBox)。要查看此 ActiveX 及其应用程序,请点击这里

现在我创建了一个新的 AciveX,用于使用 (MSDataGrid) 并在 DataGrid 的任何列中包含 (ComboBox) 和 (DTPicker) 控件。使用我的 ActiveX 控件,您可以

  • ComboBox 中选择项目来填充 DataGrid 中的任何字段。
  • 填充具有 Date 数据类型的字段。
  • 可以添加、编辑和删除记录。

背景

当使用我的 ActiveX 控件 (MKDataGrid) 时,您必须从数据库文件加载网格数据,请参见 form2form3 中的代码。我的 ActiveX 控件在下表中包含一些方法和一些属性,有关其他方法和属性,请参考 MSDataGrid

方法/属性

定义

示例

GridCaption 设置网格的标题。 MKDataGrid1.GridCaption = "学校数据库"
ColAlignment(c As Long) 设置列的对齐方式。 MKDataGrid1.ColAlignment(2) = gridLeft
ColWidth(c As Long) 设置列的宽度。 MKDataGrid1.ColWidth(0) = 150
ColumnCaption(c As Long) 设置列的标题。 MKDataGrid1. ColumnCaption (2) = "姓名"
GridFocus 如果为 True,则将焦点设置到网格 MKDataGrid1.GridFocus = True
DateControl c, b 如果 b = True,则在第 c 列设置 DTPicker 控件 MKDataGrid1.DateControl 2, True
ListControl c, rs, f 在第 c 列设置 ComboBox 控件,使用 Recordset rsField f MKDataGrid1.ListControl 4, adoCities, "CityName"

要使用我的 ActiveX (MKDataGrid),您必须从 C:\WINDOWS\SYSTEM32 文件夹中的 Interface 添加一个文件:msado20.tlb, msado21.tlb, msado25.tlb or msado27.tlb,并将 (MKDataGrid) 控件添加到工具箱。

Using the Code

LoadData() 过程

' Define following variables in General/Declarations
' Dim adoStudents As ADODB.Recordset
' Dim adoCities As ADODB.Recordset
' Dim adoCountries As ADODB.Recordset
' Dim CitySql As String
' Dim CountrySql As String

Dim cn As ADODB.Connection
Dim DataFile As String
Dim StudentSql As String
DataFile = App.Path + "\DataFiles\" + "School_E.mdb"
Set cn = New ADODB.Connection
cn.CursorLocation = adUseClient
cn.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=" & DataFile & ";"
' Load data to the grid
StudentSql = "SELECT  StudentID,StudentName,BirthDate," _
& "Address,City,Country,Phone " _
& " FROM Students " _
& "Order by StudentID"
Set adoStudents = New ADODB.Recordset
adoStudents.Open StudentSql, cn, adOpenStatic, adLockOptimistic
MKDataGrid1.DataSource = adoStudents ' Important: Not use (Set) statement her
' We shall fill the combo box with 'CityName' field
CitySql = "SELECT DISTINCTROW CityID, CityName " _
& "FROM Cities " _
& "ORDER BY CityID;"
Set adoCities = New Recordset
adoCities.Open CitySql, cn
' We shall fill the combo box with 'CountryName' field
CountrySql = "SELECT DISTINCTROW CountryID, CountryName " _
& "FROM Countries " _
& "ORDER BY CountryID;"
Set adoCountries = New Recordset
adoCountries.Open CountrySql, cn

initGrid() 过程

Dim c As Integer
set Caption to the grid
MKDataGrid1.GridCaption = "School data base"
'set columns Alignment
For c = 0 To 6
MKDataGrid1.ColAlignment(c) = gridLeft
Next c
'set column's Width
MKDataGrid1.ColWidth(0) = 1100
MKDataGrid1.ColWidth(2) = 1000
MKDataGrid1.ColWidth(3) = 2500
MKDataGrid1.ColWidth(4) = 1000
MKDataGrid1.ColWidth(5) = 1000
MKDataGrid1.ColWidth(6) = 1300
MKDataGrid1.ForeColor = QBColor(1) ' text in blue color
MKDataGrid1.HeadLines = 2 ' make column headers two lines
MKDataGrid1.HeadFont.Name = "Verdana" ' type of Head font
MKDataGrid1.HeadFont.Size = 8 ' size of Head font
MKDataGrid1.HeadFont.Bold = True ' font of Head is Bold
MKDataGrid1.Font.Name = "Times New Roman" ' type of Grid font
MKDataGrid1.Font.Size = 10 ' size of Grid font
'set column's Caption
MKDataGrid1.ColumnCaption(1) = "Student name"
MKDataGrid1.ColumnCaption(6) = "Home phone"

Form_Load() 过程

LoadData ' load data from database file
initGrid ' Caption, ColWidth, HeadFont, ...
MKDataGrid1.DateControl 2, True ' Date control at column #2
' List box at column #4 include 'CityName' field
MKDataGrid1.ListControl 4, adoCities, "CityName"
' List box at column #5 include 'CountryName' field
MKDataGrid1.ListControl 5, adoCountries, "CountryName"

您可以返回到项目 (prjDataGrid) 的源文件,以阅读比之前示例更多的内容。

备注

解压文件 MKDataGrid.zip 时,可以在文件夹 ActiveXcontrol 中找到文件 VB6DataGrid.ocx

在文件夹 DataFiles 中找到数据库文件 School_A.mdbSchool_E.mdb

找到项目 prjDataGrid,以在文件夹 MKDataGrid 中测试 ActiveX 控件。
这个项目有三个窗体

  • Form1: 用于选择语言
  • Form2: 用于英语
  • Form3: 用于阿拉伯语

Form2Form3 具有相同的控件

  • 当将文件 VB6DataGrid.ocx 添加到工具箱时,ActiveX 控件 (MKDataGrid1)
  • 按钮控件 (cmdFirst) 用于获取第一条记录。
  • 按钮控件 (cmdPrevious) 用于获取上一条记录。
  • 按钮控件 (cmdNext) 用于获取下一条记录。
  • 按钮控件 (cmdLast) 用于获取最后一条记录。
  • 按钮控件 (cmAdd) 用于添加新记录。
  • 按钮控件 (cmdEdit) 用于编辑记录。
  • 按钮控件 (cmdUpdate) 用于保存记录。
  • 按钮控件 (cmdCancel) 用于取消编辑。
  • 列表框控件 (cmdDelete) 用于删除当前行。
  • 按钮控件 (cmdRefresh) 用于排序记录。
  • 列表框控件 (cmdClose) 用于关闭窗体。

最后的话

我希望这篇文章对您有用,并能帮助您创建应用程序。如果您有任何想法或发现任何问题,请告诉我。感谢 Code Project,感谢所有人。

-- Mostafa Kaisoun
M_Kaisoun@hotmail.com

© . All rights reserved.