MSHFlexGrid的排序问题
以下的代码由VB的窗体向导生成,为什么当中的排序一定要把控件的属性设置成
固定的形式,否则只对第一列进行排序,有没有解决方法啊?
Option Explicit
Private Const MARGIN_SIZE = 60 ' 单位为缇
' 数据绑定变量
Private datPrimaryRS As ADODB.Recordset
' 能列排序变量
Private m_iSortCol As Integer
Private m_iSortType As Integer
' 列拖拽变量
Private m_bDragOK As Boolean
Private m_iDragCol As Integer
Private xdn As Integer, ydn As Integer
Private Sub Form_Load()
Dim sConnect As String
Dim sSQL As String
Dim dfwConn As ADODB.Connection
' 设置字符串
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';User ID=Admin;Data Source=E:\工作室\VB\学生管理系统\Data\Student.mdb;Mode=Share Deny None;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Database Password='';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password='';Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"
sSQL = "select Cnum,Cremark,Cscore,Cxiuxing,Snum,Tnum from Score"
' 打开连接
Set dfwConn = New Connection
dfwConn.Open sConnect
' 使用提供的集合创建 recordset
Set datPrimaryRS = New Recordset
datPrimaryRS.CursorLocation = adUseClient
datPrimaryRS.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly
Set MSHFlexGrid1.DataSource = datPrimaryRS
With MSHFlexGrid1
.Redraw = False
' 设置网格列宽度
.ColWidth(0) = -1
.ColWidth(1) = -1
.ColWidth(2) = -1
.ColWidth(3) = -1
.ColWidth(4) = -1
.ColWidth(5) = -1
' 设置网格样式
.AllowBigSelection = True
.FillStyle = flexFillRepeat
' 将标头作成粗体
.Row = 0
.Col = 0
.RowSel = .FixedRows - 1
.ColSel = .Cols - 1
.CellFontBold = True
.AllowBigSelection = False
.FillStyle = flexFillSingle
.Redraw = True
End With
End Sub
Private Sub MSHFlexGrid1_DragDrop(Source As Control, X As Single, Y As Single)
'-------------------------------------------------------------------------------------------
' 网格中 DragDrop, MouseDown, MouseMove, 和 MouseUp 事件代码能进行列拖拽
'-------------------------------------------------------------------------------------------
If m_iDragCol = -1 Then Exit Sub ' 现在不能拖拽
If MSHFlexGrid1.MouseRow <> 0 Then Exit Sub
With MSHFlexGrid1
.Redraw = False
.ColPosition(m_iDragCol) = .MouseCol
.Redraw = True
End With
End Sub
Private Sub MSHFlexGrid1_MouseDown(Button As Integer, shift As Integer, X As Single, Y As Single)
'-------------------------------------------------------------------------------------------
' 网格中 DragDrop, MouseDown, MouseMove, 和 MouseUp 事件代码能进行列拖拽
'-------------------------------------------------------------------------------------------
If MSHFlexGrid1.MouseRow <> 0 Then Exit Sub
xdn = X
ydn = Y
m_iDragCol = -1 ' 清除拖拽标志
m_bDragOK = True
End Sub
Private Sub MSHFlexGrid1_MouseMove(Button As Integer, shift As Integer, X As Single, Y As Single)
'-------------------------------------------------------------------------------------------
' 网格中 DragDrop, MouseDown, MouseMove, 和 MouseUp 事件代码能进行列拖拽
'-------------------------------------------------------------------------------------------
' 测试是否能够开始拖拽
If Not m_bDragOK Then Exit Sub
If Button <> 1 Then Exit Sub ' 错误按钮
If m_iDragCol <> -1 Then Exit Sub ' 已经开始拖拽
If Abs(xdn - X) + Abs(ydn - Y) < 50 Then Exit Sub ' 移得不够
If MSHFlexGrid1.MouseRow <> 0 Then Exit Sub ' 必须拖拽标头
' 如果到达这则开始拖拽
m_iDragCol = MSHFlexGrid1.MouseCol
MSHFlexGrid1.Drag vbBeginDrag
End Sub
Private Sub MSHFlexGrid1_MouseUp(Button As Integer, shift As Integer, X As Single, Y As Single)
'-------------------------------------------------------------------------------------------
' 网格中 DragDrop, MouseDown, MouseMove, 和 MouseUp 事件代码能进行列拖拽
'-------------------------------------------------------------------------------------------
m_bDragOK = False
End Sub
Private Sub MSHFlexGrid1_DblClick()
'-------------------------------------------------------------------------------------------
' 网格的 DblClick 事件代码能进行列排序
'-------------------------------------------------------------------------------------------
Dim i As Integer
' 仅在单击固定行时进行排序
If MSHFlexGrid1.MouseRow >= MSHFlexGrid1.FixedRows Then Exit Sub
i = m_iSortCol ' 保存旧列
m_iSortCol = MSHFlexGrid1.Col ' 设置新列
' 递增排序类型
If i <> m_iSortCol Then
' 如果在新的列上单击鼠标,开始升序排序
m_iSortType = 1
Else
' 如果在相同列单击鼠标,则进行升序和降序排序的转换。
m_iSortType = m_iSortType + 1
If m_iSortType = 3 Then m_iSortType = 1
End If
DoColumnSort
End Sub
Sub DoColumnSort()
'-------------------------------------------------------------------------------------------
' 作 Exchange-type 排序在列 m_iSortCol
'-------------------------------------------------------------------------------------------
With MSHFlexGrid1
.Redraw = False
.Row = 1
.RowSel = .Rows - 1
.Col = m_iSortCol
.Sort = m_iSortType
.Redraw = True
End With
End Sub
Private Sub Form_Resize()
Dim sngButtonTop As Single
Dim sngScaleWidth As Single
Dim sngScaleHeight As Single
On Error GoTo Form_Resize_Error
With Me
sngScaleWidth = .ScaleWidth
sngScaleHeight = .ScaleHeight
' 移动“关闭”按钮到右下角
With .cmdClose
sngButtonTop = sngScaleHeight - (.Height + MARGIN_SIZE)
.Move sngScaleWidth - (.Width + MARGIN_SIZE), sngButtonTop
End With
.MSHFlexGrid1.Move MARGIN_SIZE, _
MARGIN_SIZE, _
sngScaleWidth - (2 * MARGIN_SIZE), _
sngButtonTop - (2 * MARGIN_SIZE)
End With
Exit Sub
Form_Resize_Error:
' 避免负值错误
Resume Next
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub