下面编写了一个用记录集填充AxMSFlexGrid网格的过程。其中函数RecordCount是我自己编写的求记录集中记录的函数。这里不能直接用ADO的RecordCount求得。如果记录集是空,则退出过程。否则求出记录集的记录数和字段来确定AxMSFlexGrid网格的行列数据,然后读出记录集的数据直接填充到AxMSFlexGrid网格。要说明的是读出记录集的数据时要先判断是否为空值,由函数IsDBNull实现(函数IsDBNull来源于System.DBNull).最后记录集应该返回到记录首位,否则影响了原有的记录集,完整的代码如下:
'作用:用记录集的数据填充网格
'参数:MSGrid 网格对象,rec 记录集对象
Public Sub FillMsGrid(ByVal MSGrid As AxMSFlexGridLib.AxMSFlexGrid, ByVal rec As ADODB.Recordset)
Dim i, j, RecordNum As Integer
If rec.EOF Then Exit Sub
RecordNum = RecordCount(rec)
MSGrid.Rows = RecordNum + 1
MSGrid.Cols = rec.Fields.Count + 1
For i = 0 To RecordNum - 1
For j = 0 To rec.Fields.Count - 1
If IsDBNull(rec(j).Value) Then
MSGrid.set_TextMatrix(i + 1, j + 1, "")
Else
MSGrid.set_TextMatrix(i + 1, j + 1, rec(j).Value)
End If
Next
MSGrid.set_TextMatrix(i + 1, 0, i)
rec.MoveNext()
Next
rec.MoveFrist()
End Sub
'作用:取记录集的记录数
'参数:rec 记录集对象
'返回:记录集的记录数
Public Function RecordCount(ByVal rec As ADODB.Recordset) As Integer
Dim i As Integer
If rec.EOF Then
RecordCount = 0
Exit Function
End If
With rec
.MoveFirst()
Do While Not .EOF
i += 1
.MoveNext()
Loop
.MoveFirst()
End With
RecordCount = i
End Function
'参数:MSGrid 网格对象,rec 记录集对象
Public Sub FillMsGrid(ByVal MSGrid As AxMSFlexGridLib.AxMSFlexGrid, ByVal rec As ADODB.Recordset)
Dim i, j, RecordNum As Integer
If rec.EOF Then Exit Sub
RecordNum = RecordCount(rec)
MSGrid.Rows = RecordNum + 1
MSGrid.Cols = rec.Fields.Count + 1
For i = 0 To RecordNum - 1
For j = 0 To rec.Fields.Count - 1
If IsDBNull(rec(j).Value) Then
MSGrid.set_TextMatrix(i + 1, j + 1, "")
Else
MSGrid.set_TextMatrix(i + 1, j + 1, rec(j).Value)
End If
Next
MSGrid.set_TextMatrix(i + 1, 0, i)
rec.MoveNext()
Next
rec.MoveFrist()
End Sub
'作用:取记录集的记录数
'参数:rec 记录集对象
'返回:记录集的记录数
Public Function RecordCount(ByVal rec As ADODB.Recordset) As Integer
Dim i As Integer
If rec.EOF Then
RecordCount = 0
Exit Function
End If
With rec
.MoveFirst()
Do While Not .EOF
i += 1
.MoveNext()
Loop
.MoveFirst()
End With
RecordCount = i
End Function
以上代码编好后放在所定义的类中.下面的省略号代表上面的函数和过程。可直接生成为DLL组件。方法是在.NET编辑环境下选择生成菜单中按生成就生成了DLL文件。然后,你可以直接调用该组件了。
Imports ADODB.Connection
Imports ADODB.Recordset
Imports ADODB.CursorLocationEnum
Imports System.DBNull '函数IsDBNull的来源
Class ADOAccess
Private mCnnDB As New ADODB.Connection()
…
End Class
Imports ADODB.Recordset
Imports ADODB.CursorLocationEnum
Imports System.DBNull '函数IsDBNull的来源
Class ADOAccess
Private mCnnDB As New ADODB.Connection()
…
End Class
调用上面生成的组件方法如下:在Visual Basic.NET中建立一项目,在窗口Form1中加入一AxMSFlexGrid网格命名为MsGrid1,布置如下
然后引用刚建立的Dll。方法是选择项目菜单的添加引用,选择项目页,按浏览找到其DLL文件确定后引用完成。在Form1中定义一ADOAccess对象,在Load事件中编写调用代码,分别测试其功能,代码如下:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim DB As New ADOAccess()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rec As New ADODB.Recordset()
'DB.ConnAccess("e:\test.mdb")
'DB.ConnSQLServer("wj-1058", "test")
'DB.ConnODBC("testODBC")
'DB.ConnOracle("wj-1059", "system", "manager")
'rec = DB.OpenTable("DEMO.customer")
rec = DB.OpenTable("doc_file")
DB.FillMsGrid(MSGrid1, rec)
End Sub
Inherits System.Windows.Forms.Form
Dim DB As New ADOAccess()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rec As New ADODB.Recordset()
'DB.ConnAccess("e:\test.mdb")
'DB.ConnSQLServer("wj-1058", "test")
'DB.ConnODBC("testODBC")
'DB.ConnOracle("wj-1059", "system", "manager")
'rec = DB.OpenTable("DEMO.customer")
rec = DB.OpenTable("doc_file")
DB.FillMsGrid(MSGrid1, rec)
End Sub