الثلاثاء، 14 ديسمبر 2010

how to Sort the data in an MSFlexGrid control



In the control's MouseUp event handler, use its MouseRow function to see if the user clicked in the first row. If this is the first row, call subroutine SortByColumn, passing it the column clicked.

Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As _
Integer, x As Single, y As Single)
' If this is not row 0, do nothing.
If MSFlexGrid1.MouseRow <> 0 Then Exit Sub

' Sort by the clicked column.
SortByColumn MSFlexGrid1.MouseCol
End Sub


Subroutine SortByColumn sets the FlexGrid's Col property to the column that it should use for sorting. It calls the control's Sort method telling it whether to sort ascending or descending. It also adds a < or > to the sort column's heading to indicate the sort order.



' Sort by the indicated column.
Private Sub SortByColumn(ByVal sort_column As Integer)
' Hide the FlexGrid.
MSFlexGrid1.Visible = False
MSFlexGrid1.Refresh

' Sort using the clicked column.
MSFlexGrid1.Col = sort_column
MSFlexGrid1.ColSel = sort_column
MSFlexGrid1.Row = 0
MSFlexGrid1.RowSel = 0
' If this is a new sort column, sort ascending.
' Otherwise switch which sort order we use.
If m_SortColumn <> sort_column Then
m_SortOrder = flexSortGenericAscending
ElseIf m_SortOrder = flexSortGenericAscending Then
m_SortOrder = flexSortGenericDescending
Else
m_SortOrder = flexSortGenericAscending
End If
MSFlexGrid1.Sort = m_SortOrder

' Restore the previous sort column's name.
If m_SortColumn >= 0 Then
MSFlexGrid1.TextMatrix(0, m_SortColumn) = _
Mid$(MSFlexGrid1.TextMatrix(0, m_SortColumn), 3)
End If

' Display the new sort column's name.
m_SortColumn = sort_column
If m_SortOrder = flexSortGenericAscending Then
MSFlexGrid1.TextMatrix(0, m_SortColumn) = "> " & _
MSFlexGrid1.TextMatrix(0, m_SortColumn)
Else
MSFlexGrid1.TextMatrix(0, m_SortColumn) = "< " & _ MSFlexGrid1.TextMatrix(0, m_SortColumn)
 End If ' Display the FlexGrid.
 MSFlexGrid1.Visible = True
End Sub



 Special Trick
 Apparently, if a column's data begins with a digit, the control tries to sort the column numerically. If the data isn't really numeric, such as an ISBN in this case (e.g. "1-59059-121-6"), the sort fails. You can get around this by adding a space to the beginning of the data (e.g. " 1-59059-121-6") so the control doesn't try to treat it as numeric.

ليست هناك تعليقات:

إرسال تعليق