下面编写了一个用DataReader填充AxMSFlexGrid网格的过程。由于DataReader是只向后读取数据的方法,它每读取一条数据要通过Read实现,当读完时Read返回false.读数据时要考虑读出的数据是否为空值,我们可以灵活地把转换为字符串来实现. AxMSFlexGrid的列数应该首先确定。每向AxMSFlexGrid填充一条记录,AxMSFlexGrid应该增加一项,可通过AddItem实现。完整的代码如下:
作用:用记录集的数据填充网格
参数:MSGrid 网格对象,DataReader 数据读者
代码:list1
作用:用数据表数据填充网格
参数:MSGrid 网格对象,TableName 表名
代码:list2
作用:用数据表数据填充网格
参数:MSGrid 网格对象,TableName 表名, IsID表格是否增加自然顺序号
代码:list3
作用:用数据表数据填充网格
参数:MSGrid 网格对象,strSQL
查询语句
代码:
Public Overloads Sub FillDataGrid(ByVal GridName As DataGrid, ByVal strSQL As String)
GridName.DataSource = SQLDataTable(strSQL)
End Sub
由表名得到数据表:
Public Overloads Function GetDataTable(ByVal TableName As String) As DataTable
Dim myDataSet As New DataSet()
myDataSet = GetDataSet(TableName)
Return myDataSet.Tables(0)
End Function
由表名和条件得到数据表:
Public Overloads Function GetDataTable(ByVal TableName As String, ByVal strWhere As String) As DataTable
Dim myDataSet As New DataSet()
myDataSet = GetDataSet(TableName, strWhere)
Return myDataSet.Tables(0)
End Function
由表名得到数据视图:
Public Overloads Function GetDataView(ByVal TableName As String) As DataView
Dim myDataSet As New DataSet()
myDataSet = GetDataSet(TableName)
Return myDataSet.Tables(0).DefaultView
End Function
由表名和条件得到数据视图:
Public Overloads Function GetDataView(ByVal TableName As String, ByVal strWhere As String) As DataView
Dim myDataSet As New DataSet()
myDataSet = GetDataSet(TableName, strWhere)
Return myDataSet.Tables(0).DefaultView
End Function
由查询语句得到数据集:
Public Overloads Function SQLDataSet(ByVal strSQL As String) As DataSet
Dim myDataSet As New DataSet()
Dim myOleDataAdapter As New OleDbDataAdapter()
myOleDataAdapter.SelectCommand = New OleDbCommand(strSQL, mOleCnnDB)
myOleDataAdapter.Fill(myDataSet)
Return myDataSet
End Function
由查询语句得到数据表:
Public Overloads Function SQLDataTable(ByVal strSQL As String) As DataTable
Dim myDataSet As New DataSet()
myDataSet = SQLDataSet(strSQL)
Return myDataSet.Tables(0)
End Function
由查询语句得到数据视图:
Public Overloads Function SQLDataView(ByVal strSQL As String) As DataView
Dim myDataSet As New DataSet()
myDataSet = SQLDataSet(strSQL)
Return myDataSet.Tables(0).DefaultView
End Function