用MouseDown试试Private Sub Grid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Grid1.Row <> 1 Then Exit Sub Grid1.Sort = 1 End Sub
Private Sub MSHFlexGrid1_RowColChange() MSHFlexGrid1.Sort = 1 End Sub
新建vb工程->添加窗体->vb数据窗体向导-Biblio.mdb(或其它一个Access数据库)->MS HFlexGrid->...->选择允许用户排序->..,完成。生成新窗口,查看代码。以下是代码的部分内容:' 能列排序变量 Private m_iSortCol As Integer Private m_iSortType As IntegerPrivate 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 DoColumnSortEnd 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 WithEnd Sub
Private Sub MSHFlexGrid1_Click() if mousecol=0 then'检测鼠标单击列标题 select case mouserow case 1 adodc1="Select * From 新食物成份表 Order By 新食物成份表.食物名"'此处为按第一列进行排序的查询语句,其余类推 adodc1.Refresh Set MSHFlexGrid1.DataSource = Adodc1 MSHFlexGrid1.Refresh ...... End Sub 你试试吧,我没试过,思路应该没问题。
有点小错误,试过以后,是这样的: Private Sub MSHFlexGrid1_Click()If MSHFlexGrid1.MouseRow = 0 Then '检测鼠标单击列标题 Select Case MSHFlexGrid1.MouseCol Case 1 Adodc1.RecordSource = "Select......" Adodc1.Refresh Set MSHFlexGrid1.DataSource = Adodc1 MSHFlexGrid1.Refresh ...... End Select End If End Sub
以下代码能实现你要的功能:Private Sub MSFlexGrid1_Click() With MSFlexGrid1 If .MouseCol = 0 Then MSFlexGrid1.Col = 0 MSFlexGrid1.Sort = 2 End If If .MouseCol = 1 Then MSFlexGrid1.Col = 1 MSFlexGrid1.Sort = 1 End If If .MouseCol = 2 Then MSFlexGrid1.Col = 2 MSFlexGrid1.Sort = 1 End If
If .MouseCol = 3 Then MSFlexGrid1.Col = 3 MSFlexGrid1.Sort = 1 End If If .MouseCol = 4 Then MSFlexGrid1.Col = 4 MSFlexGrid1.Sort = 1 End If If .MouseCol = 5 Then MSFlexGrid1.Col = 5 MSFlexGrid1.Sort = 1 End If If .MouseCol = 6 Then MSFlexGrid1.Col = 6 MSFlexGrid1.Sort = 1 End If End With End Sub
Private Sub Form_Load() Me.MSFlexGrid1.Rows = 4 Me.MSFlexGrid1.Cols = 15 Me.MSFlexGrid1.TextMatrix(0, 0) = "aa" Me.MSFlexGrid1.TextMatrix(0, 1) = "bb" Me.MSFlexGrid1.TextMatrix(0, 2) = "cc" Me.MSFlexGrid1.TextMatrix(0, 3) = "dd" Me.MSFlexGrid1.TextMatrix(1, 0) = "12" Me.MSFlexGrid1.TextMatrix(1, 1) = "13" Me.MSFlexGrid1.TextMatrix(1, 2) = "56" Me.MSFlexGrid1.TextMatrix(1, 3) = "76" Me.MSFlexGrid1.TextMatrix(2, 0) = "122" Me.MSFlexGrid1.TextMatrix(2, 1) = "123" Me.MSFlexGrid1.TextMatrix(2, 2) = "526" Me.MSFlexGrid1.TextMatrix(2, 3) = "726" Me.MSFlexGrid1.TextMatrix(3, 0) = "192" Me.MSFlexGrid1.TextMatrix(3, 1) = "193" Me.MSFlexGrid1.TextMatrix(3, 2) = "596" Me.MSFlexGrid1.TextMatrix(3, 3) = "796" End SubPrivate Sub MSFlexGrid1_Click() With MSFlexGrid1 If .MouseCol = 0 Then MSFlexGrid1.Col = 0 MSFlexGrid1.Sort = 2 End If If .MouseCol = 1 Then MSFlexGrid1.Col = 1 MSFlexGrid1.Sort = 1 End If If .MouseCol = 2 Then MSFlexGrid1.Col = 2 MSFlexGrid1.Sort = 2 End If
If .MouseCol = 3 Then MSFlexGrid1.Col = 3 MSFlexGrid1.Sort = 1 End If If .MouseCol = 4 Then MSFlexGrid1.Col = 4 MSFlexGrid1.Sort = 2End If End With End Sub
我把楼上的程序改成这样,但不明白有的时候明明点了第0行,但为什么取出来的MSFlexGrid1.MouseRow却是2或者3。 Dim bolChange As BooleanPrivate Sub Form_Load() Me.MSFlexGrid1.Rows = 4 Me.MSFlexGrid1.Cols = 15 Me.MSFlexGrid1.TextMatrix(0, 0) = "aa" Me.MSFlexGrid1.TextMatrix(0, 1) = "bb" Me.MSFlexGrid1.TextMatrix(0, 2) = "cc" Me.MSFlexGrid1.TextMatrix(0, 3) = "dd" Me.MSFlexGrid1.TextMatrix(1, 0) = "12" Me.MSFlexGrid1.TextMatrix(1, 1) = "13" Me.MSFlexGrid1.TextMatrix(1, 2) = "56" Me.MSFlexGrid1.TextMatrix(1, 3) = "76" Me.MSFlexGrid1.TextMatrix(2, 0) = "122" Me.MSFlexGrid1.TextMatrix(2, 1) = "123" Me.MSFlexGrid1.TextMatrix(2, 2) = "526" Me.MSFlexGrid1.TextMatrix(2, 3) = "726" Me.MSFlexGrid1.TextMatrix(3, 0) = "192" Me.MSFlexGrid1.TextMatrix(3, 1) = "193" Me.MSFlexGrid1.TextMatrix(3, 2) = "596" Me.MSFlexGrid1.TextMatrix(3, 3) = "796" End SubPrivate Sub MSFlexGrid1_Click() If MSFlexGrid1.MouseRow = 0 Then Call SortChange(MSFlexGrid1.MouseCol, bolChange) End If End SubPrivate Sub SortChange(ByVal CurCol As Long, ByRef bolSort As Boolean) With MSFlexGrid1 bolSort = Not (bolSort) If CurCol = CurCol Then .Col = CurCol If bolSort = True Then .Sort = 2 Else .Sort = 1 End If End If End With End Sub
lstYX.SortKey = ColumnHeader.Index - 1
lstYX.Sorted = True
End Sub
Private Sub Grid1_Click()
If Grid1.Row <> 0 Then Exit Sub
Grid1.Sort = 1
End Sub
Private Sub Grid1_Click()
If Grid1.Row <> 1 Then Exit Sub
Grid1.Sort = 1
End Sub
还有就是,即便排序了也是值排序一次,如果点不同的标题,并没有按那个标题进行重新排序啊。有没有解决办法呢?
If Grid1.Row <> 1 Then Exit Sub
Grid1.Sort = 1
End Sub
MSHFlexGrid1.Sort = 1
End Sub
Private m_iSortCol As Integer
Private m_iSortType As IntegerPrivate 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 DoColumnSortEnd 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 WithEnd Sub
现在发现排序到是能排了,但是点击别的列的时候,还是按原来第一列牌的序,请你们看一下,到底是不是?该怎么办啊?还有chen22(chen22) 提供的方法里面的排序功能也是只能按一列排序啊!
请给指点指点!谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了
if mousecol=0 then'检测鼠标单击列标题
select case mouserow
case 1
adodc1="Select * From 新食物成份表 Order By 新食物成份表.食物名"'此处为按第一列进行排序的查询语句,其余类推
adodc1.Refresh
Set MSHFlexGrid1.DataSource = Adodc1
MSHFlexGrid1.Refresh
......
End Sub
你试试吧,我没试过,思路应该没问题。
Private Sub MSHFlexGrid1_Click()If MSHFlexGrid1.MouseRow = 0 Then '检测鼠标单击列标题
Select Case MSHFlexGrid1.MouseCol
Case 1
Adodc1.RecordSource = "Select......"
Adodc1.Refresh
Set MSHFlexGrid1.DataSource = Adodc1
MSHFlexGrid1.Refresh
......
End Select
End If
End Sub
With MSFlexGrid1
If .MouseCol = 0 Then
MSFlexGrid1.Col = 0
MSFlexGrid1.Sort = 2
End If
If .MouseCol = 1 Then
MSFlexGrid1.Col = 1
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 2 Then
MSFlexGrid1.Col = 2
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 3 Then
MSFlexGrid1.Col = 3
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 4 Then
MSFlexGrid1.Col = 4
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 5 Then
MSFlexGrid1.Col = 5
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 6 Then
MSFlexGrid1.Col = 6
MSFlexGrid1.Sort = 1
End If
End With
End Sub
另:您可给小妹来份信,小妹给你个例子.
[email protected]
[email protected]
Me.MSFlexGrid1.Rows = 4
Me.MSFlexGrid1.Cols = 15
Me.MSFlexGrid1.TextMatrix(0, 0) = "aa"
Me.MSFlexGrid1.TextMatrix(0, 1) = "bb"
Me.MSFlexGrid1.TextMatrix(0, 2) = "cc"
Me.MSFlexGrid1.TextMatrix(0, 3) = "dd"
Me.MSFlexGrid1.TextMatrix(1, 0) = "12"
Me.MSFlexGrid1.TextMatrix(1, 1) = "13"
Me.MSFlexGrid1.TextMatrix(1, 2) = "56"
Me.MSFlexGrid1.TextMatrix(1, 3) = "76"
Me.MSFlexGrid1.TextMatrix(2, 0) = "122"
Me.MSFlexGrid1.TextMatrix(2, 1) = "123"
Me.MSFlexGrid1.TextMatrix(2, 2) = "526"
Me.MSFlexGrid1.TextMatrix(2, 3) = "726"
Me.MSFlexGrid1.TextMatrix(3, 0) = "192"
Me.MSFlexGrid1.TextMatrix(3, 1) = "193"
Me.MSFlexGrid1.TextMatrix(3, 2) = "596"
Me.MSFlexGrid1.TextMatrix(3, 3) = "796"
End SubPrivate Sub MSFlexGrid1_Click()
With MSFlexGrid1
If .MouseCol = 0 Then
MSFlexGrid1.Col = 0
MSFlexGrid1.Sort = 2
End If
If .MouseCol = 1 Then
MSFlexGrid1.Col = 1
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 2 Then
MSFlexGrid1.Col = 2
MSFlexGrid1.Sort = 2
End If
If .MouseCol = 3 Then
MSFlexGrid1.Col = 3
MSFlexGrid1.Sort = 1
End If
If .MouseCol = 4 Then
MSFlexGrid1.Col = 4
MSFlexGrid1.Sort = 2End If
End With
End Sub
Dim bolChange As BooleanPrivate Sub Form_Load()
Me.MSFlexGrid1.Rows = 4
Me.MSFlexGrid1.Cols = 15
Me.MSFlexGrid1.TextMatrix(0, 0) = "aa"
Me.MSFlexGrid1.TextMatrix(0, 1) = "bb"
Me.MSFlexGrid1.TextMatrix(0, 2) = "cc"
Me.MSFlexGrid1.TextMatrix(0, 3) = "dd"
Me.MSFlexGrid1.TextMatrix(1, 0) = "12"
Me.MSFlexGrid1.TextMatrix(1, 1) = "13"
Me.MSFlexGrid1.TextMatrix(1, 2) = "56"
Me.MSFlexGrid1.TextMatrix(1, 3) = "76"
Me.MSFlexGrid1.TextMatrix(2, 0) = "122"
Me.MSFlexGrid1.TextMatrix(2, 1) = "123"
Me.MSFlexGrid1.TextMatrix(2, 2) = "526"
Me.MSFlexGrid1.TextMatrix(2, 3) = "726"
Me.MSFlexGrid1.TextMatrix(3, 0) = "192"
Me.MSFlexGrid1.TextMatrix(3, 1) = "193"
Me.MSFlexGrid1.TextMatrix(3, 2) = "596"
Me.MSFlexGrid1.TextMatrix(3, 3) = "796"
End SubPrivate Sub MSFlexGrid1_Click()
If MSFlexGrid1.MouseRow = 0 Then
Call SortChange(MSFlexGrid1.MouseCol, bolChange)
End If
End SubPrivate Sub SortChange(ByVal CurCol As Long, ByRef bolSort As Boolean)
With MSFlexGrid1
bolSort = Not (bolSort)
If CurCol = CurCol Then
.Col = CurCol
If bolSort = True Then
.Sort = 2
Else
.Sort = 1
End If
End If
End With
End Sub