Private Sub Form_Load()
MSFlexGrid1.Cols = 5
MSFlexGrid1.Rows = 10
MSFlexGrid1.FixedRows = Int(InputBox("请输入固定行的行数:", "提示", 3))
End Sub
Private Sub MSFlexGrid1_Click()
If MSFlexGrid1.MouseRow >= 0 And MSFlexGrid1.MouseRow < MSFlexGrid1.FixedRows Then MsgBox "你单击了固定行(的第" & MSFlexGrid1.MouseRow + 1 & "行)!"
End Sub
Dim curRow As Long, curCol As Long
Private Sub MSHFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
With MSHFlexGrid1
curRow = .Row
curCol = .Col
.Row = .Rows - 1
.Col = .cols - 1
If .ColPos(.cols - 1) + .CellWidth < x Or .RowPos(.Rows - 1) + .CellHeight < y Then
MsgBox "点在空白区"
Else
If .MouseRow = 0 Then
MsgBox "点中固定行"
ElseIf .MouseCol = 0 Then
MsgBox "点中固定列"
Else
MsgBox "数据区"
End If
End If
.Row = curRow
.Col = curCol
End With
End Sub
Private Sub MSHFlexGrid1_Click()
If MSHFlexGrid1.MouseRow = 0 Then
MsgBox "点中固定行"
ElseIf MSHFlexGrid1.MouseCol = 0 Then
MsgBox "点中固定列"
Else
MsgBox "非固定行列"
End If
End Sub
'设置网格控件绑定方法
Private Sub MSHFGridBind()
Dim i, j As Integer '循环计数变量
With MSHFlexGrid1
.Rows = rs.RecordCount + 1
.Cols = rs.Fields.Count + 1
.FixedRows = 0
End With
'设置第一行单元格背景颜色(假固定行)
For i = 1 To MSHFlexGrid1.Cols - 1
MSHFlexGrid1.CellBackColor = RGB(212, 208, 200)
Next
'设置第一行单元格内容为字段名
For i = 0 To rs.Fields.Count - 1
MSHF.TextMatrix(0, i + 1) = rs(i).Name
Next i
'设置第二行起单元格内容为字段值,先行后列
For j = 1 To rs.RecordCount
If rs.EOF Then Exit Sub
For i = 0 To rs.Fields.Count - 1
'如果数据库中值为空,则赋值空字符串至网格控件内
If IsNull(rs(i).Value) Then
MSHF.TextMatrix(j, i + 1) = " "
Else
MSHF.TextMatrix(j, i + 1) = rs(i).Value
End If
Next i
rs.MoveNext
Next j
End Sub