回复 3楼wlosg的帖子
2.通过ADO绑定数据源时,如下代码所示:
'设置数据源
Ado.ConnectionString = SQLStr
Ado.RecordSource = "SELECT * FROM TestTable where " & lstr
Ado.Refresh
Set fpSpread1.DataSource = Ado
(1).数据源数据加载到表格后,如何断开与数据源数据的连接?
可以把数据库连接关掉,先判断 ADODB.Connection 的 State,如果为 0 则关闭:
- Dim dbpath As String
- dbpath = App.Path & "\common\nwind.mdb"
-
- Set adoconn1 = New ADODB.Connection
- adoconn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Persist Security Info=False"
-
- 'Populate grid with sample data
- Set rs = New ADODB.Recordset
- rs.Open "Select top 100 * from Orders", adoconn1, adOpenStatic, adLockOptimistic
- Set fpSpread1.DataSource = rs
-
- If adoconn1.State = 0 Then
- adoconn1.Close
- End If
复制代码
(2)通过 SearchRow 方法获取所在列:
- Private Sub Command2_Click()
- Dim field As String '
- field = "测试"
- Dim col As Integer
- col = Me.fpSpread1.SearchRow(0, 0, 100, field, SearchFlagsValue)
-
- End Sub
复制代码
(3).如何通过一列的字段信息来设置行和列的颜色,如有一个结果列,字段信息中只有PASS、FAIL两种状态,我想设置PASS的时候行是红色,FAIL的时候是绿色,有没有什么比较快的方法?
可以通过遍历单元格文本来判断,Spread COM 版没有提供条件格式化功能:
- Private Sub Command2_Click()
- Dim i As Integer
- i = 0
- Dim rowcount As Integer
- rowcount = Me.fpSpread1.MaxRows
- Dim text As String
- For i = 0 To rowcount
- Me.fpSpread1.row = i
- Me.fpSpread1.col = 2
-
- text = Me.fpSpread1.text
- If text = "FAIL" Then
- Me.fpSpread1.BackColor = RGB(0, 0, 255)
- ElseIf text = "PASS" Then
- Me.fpSpread1.BackColor = RGB(0, 255, 0)
- End If
-
- Next i
- End Sub
复制代码
(4).还有一个问题就是,我的数据库字段有一个时间字段信息,类型为datetime格式(即如:2013-08-01 12:00:01),但是在表格中显示时,确只显示2013-08-01,时间信息没有显示出来是为什么?
Spread COM 版的 CellTypeDate 仅支持显示到日期,无法显示时间。 |