在vb6中要显示数据虽然有datagrid、msflexgrid、mshflexgrid、vsflexgrid、True dbgrid7.0 可选,不过我在工作中用的最多的还是MSHFlexGrid,以下我会常分享一些使用这个控件的技巧、方法代码,保证拿了就可以用。
1、使用MSHFlexGrid的FormatString属性可以做到一次设置行标题和列标题
Dim sTitle As String
sTitle = "<Name |<Address |<Telephone |<Social Security>"
sTitle = sTitle + ";|Robert|Jimmy|Bonzo|John Paul"
MSHFlexGrid.FormatString = sTitle
Dim sTitle As String '列标题
'设置数据源
Set msh_Data.DataSource = AllRs
msh_Data.Refresh
'///解决不能单击鼠标指向行///
If msh_Data.Rows > 1 Then
msh_Data.FixedRows = 0
msh_Data.FixedRows = 1
End If
With msh_Data
'填充左边记录行数
.TextMatrix(0, 0) = " 序号"
Dim i As Long
For i = .FixedRows To .Rows - .FixedRows
.TextMatrix(i, 0) = i
Next i
.RowHeight(0) = 600 '设置首行也即标题栏高度
'单击选择整行
.FocusRect = flexFocusNone
.SelectionMode = flexSelectionByRow
'msh_Data.BackColorSel = vbYellow
'固定第一列,不然再移动下一列的时候就会自动跳到最后一列了
.Col = 1
.FormatString = sTitle '设置列标题内容
.ColWidth(0) = 0
.ColWidth(1) = 600
End With
'刷新后选取首行
msh_Data.Row = msh_Data.FixedRows
msh_Data.RowSel = msh_Data.FixedRows
msh_Data.Col = 0
msh_Data.ColSel = msh_Data.Cols - 1
'刷新后选取最后一行
MSFlexGrid1.Row = MSFlexGrid1.Rows - MSFlexGrid1.FixedRows
MSFlexGrid1.RowSel = MSFlexGrid1.Rows - MSFlexGrid1.FixedRows
MSFlexGrid1.Col = 0
MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
'禁止mshflexgrid选择多行记录
'第一种方法
Private Sub MSHFlexGrid1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If MSHFlexGrid1.RowSel <> MSHFlexGrid1.Row Then MSHFlexGrid1.RowSel = MSHFlexGrid1.Row
End Sub
'第二种方法
Private Sub msh_Data_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If msh_Data.MouseRow = 0 Then Exit Sub
With msh_Data
.Row = .MouseRow
CURRENTROW = .Row
.Col = 0 '如果是0则可以不选择多行
.ColSel = .Cols - 1
End With
End Sub
Private Sub msh_Data_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If msh_Data.MouseRow = 0 Then Exit Sub
With msh_Data
.RowSel = CURRENTROW
.ColSel = .Cols - 1
End With
End Sub