|
-
Aug 3rd, 2005, 02:35 PM
#2
Re: FlexGrid Sorting/Rearranging Columns
To answer your question on sorting by a certain column, you can use this general routine (I would place this in a module) :
VB Code:
'-----------------------------------------------------------------------------
Public Sub SortGrid(pobjGrid As MSFlexGrid, ByVal plngSortCol As Long)
'-----------------------------------------------------------------------------
With pobjGrid
' suppress changes in display until done
.Redraw = False
' Sort using the clicked column.
.Col = plngSortCol
.ColSel = plngSortCol
.Row = 0
.RowSel = 0
' If this is a new sort column, sort ascending.
' Otherwise switch which sort order we use.
If mlngSortCol <> plngSortCol Then
mintSortOrder = flexSortGenericAscending
ElseIf mintSortOrder = flexSortGenericAscending Then
mintSortOrder = flexSortGenericDescending
Else
mintSortOrder = flexSortGenericAscending
End If
.Sort = mintSortOrder
' Restore the previous sort column's name.
If mlngSortCol = 0 Then
If Left$(.TextMatrix(0, 0), 2) = "< " _
Or Left$(.TextMatrix(0, 0), 2) = "> " _
Then
.TextMatrix(0, 0) = Mid$(.TextMatrix(0, 0), 3)
End If
Else
.TextMatrix(0, mlngSortCol) = Mid$(.TextMatrix(0, mlngSortCol), 3)
End If
' Display the new sort column's name.
mlngSortCol = plngSortCol
If mintSortOrder = flexSortGenericAscending Then
.TextMatrix(0, mlngSortCol) = "< " & _
.TextMatrix(0, mlngSortCol)
Else
.TextMatrix(0, mlngSortCol) = "> " & _
.TextMatrix(0, mlngSortCol)
End If
' refresh the display
.Redraw = True
End With
End Sub
USAGE:
VB Code:
'-----------------------------------------------------------------------------
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
MSFlexGrid1.Highlight = flexHighlightNever
' Sort by the clicked column.
SortGrid MSFlexGrid1, MSFlexGrid1.MouseCol
End Sub
NOTES:
When setting up the grid, it is assumed that you have set FixedRows to 1 (thus reserving the first row, row 0, for the column headings). In my example, I am not using any fixed columns, but if you are, than you should test for that in your MouseUp routine, i.e.:
If MSFlexGrid1.MouseCol = 0 Then Exit Sub
Also, as a rule of thumb, if the column contains numeric data, concatenate one blank space at the beginning of the data, i.e.:
" " & TheNumericData
Additionally, for dates, format them with the year first:
" " & Format$(TheDateField, "yyyy-mm-dd")
-----------------------------------------------------------------
Can't help you with rearranging columns
----------------------------------------------------------------
To set the alignment of text in the current cell, you can use the CellAlignment property.
I hope this has helped.
"It's cold gin time again ..."
Check out my website here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|