Results 1 to 13 of 13

Thread: FlexGrid Sorting/Rearranging Columns

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    FlexGrid Sorting/Rearranging Columns

    I think I finally figured out how to use the FlexGrid control. I have been told that you can’t sort by column in a FlexGrid, but there is a .Sort property. I was wondering how to use that property. I saw the constants in the Object Browser, but how do I apply the property to a certain column? Or can it only be applied to the left most column? In the Object Broswer it says is sorts selected rows, does that mean I can’t sort the entire grid by column?

    Which brings me to my next question: how do I rearrange columns? Like if someone drags a column heading to the other side of the grid I want it to drop there and all the text in that column to move with it. Would I have to manually write all the code including rewriting the whole text matrix?

    Also, how do I get the text in the upper cell of a FlexGrid to be left aligned?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    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:
    1. '-----------------------------------------------------------------------------
    2. Public Sub SortGrid(pobjGrid As MSFlexGrid, ByVal plngSortCol As Long)
    3. '-----------------------------------------------------------------------------
    4.    
    5.     With pobjGrid
    6.    
    7.         ' suppress changes in display until done
    8.         .Redraw = False
    9.    
    10.         ' Sort using the clicked column.
    11.         .Col = plngSortCol
    12.         .ColSel = plngSortCol
    13.         .Row = 0
    14.         .RowSel = 0
    15.    
    16.         ' If this is a new sort column, sort ascending.
    17.         ' Otherwise switch which sort order we use.
    18.         If mlngSortCol <> plngSortCol Then
    19.             mintSortOrder = flexSortGenericAscending
    20.         ElseIf mintSortOrder = flexSortGenericAscending Then
    21.             mintSortOrder = flexSortGenericDescending
    22.         Else
    23.             mintSortOrder = flexSortGenericAscending
    24.         End If
    25.         .Sort = mintSortOrder
    26.    
    27.         ' Restore the previous sort column's name.
    28.         If mlngSortCol = 0 Then
    29.             If Left$(.TextMatrix(0, 0), 2) = "< " _
    30.             Or Left$(.TextMatrix(0, 0), 2) = "> " _
    31.             Then
    32.                 .TextMatrix(0, 0) = Mid$(.TextMatrix(0, 0), 3)
    33.             End If
    34.         Else
    35.             .TextMatrix(0, mlngSortCol) = Mid$(.TextMatrix(0, mlngSortCol), 3)
    36.         End If
    37.    
    38.         ' Display the new sort column's name.
    39.         mlngSortCol = plngSortCol
    40.         If mintSortOrder = flexSortGenericAscending Then
    41.             .TextMatrix(0, mlngSortCol) = "< " & _
    42.                 .TextMatrix(0, mlngSortCol)
    43.         Else
    44.             .TextMatrix(0, mlngSortCol) = "> " & _
    45.                 .TextMatrix(0, mlngSortCol)
    46.         End If
    47.    
    48.        ' refresh the display
    49.         .Redraw = True
    50.    
    51.     End With
    52.    
    53. End Sub

    USAGE:
    VB Code:
    1. '-----------------------------------------------------------------------------
    2. Private Sub MSFlexGrid1_MouseUp(Button As Integer, _
    3.                                 Shift As Integer, _
    4.                                 X As Single, _
    5.                                 Y As Single)
    6. '-----------------------------------------------------------------------------
    7.    
    8.     ' If this is not row 0, do nothing.
    9.     If MSFlexGrid1.MouseRow <> 0 Then Exit Sub
    10.  
    11.     MSFlexGrid1.Highlight = flexHighlightNever
    12.    
    13.     ' Sort by the clicked column.
    14.     SortGrid MSFlexGrid1, MSFlexGrid1.MouseCol
    15.  
    16. 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.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: FlexGrid Sorting/Rearranging Columns

    You can sort by column, in fact you can sort by multiple columns.

    When the Sort property is called, data is sorted by the currentl column(s). The order is always left to right and you can only sort by contiguous columns.

    VB Code:
    1. 'code to sort by columns 1 and 2
    2.  With FlexGrid
    3.      .Col = 1
    4.      .ColSel = 2
    5.      .Sort = flexSortStringAscending
    6. End With

    To align columns or individual cells
    VB Code:
    1. With FlexGrid
    2.      .ColAlignment(1) = flexAlignLeftCenter 'align entire column
    3.      .Col = 1
    4.      .Row = 7
    5.      .CellAlignment = flexAlignRightCenter 'cell alignment overrides column alignment
    6. End With

    To Move a column, change its ColPosition

    VB Code:
    1. Private lngMoveColumn as Long
    2.  
    3. Private Sub grdDetails_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    4.     If grdDetails.MouseRow = 0 And grdDetails.MouseCol >= grdDetails.FixedCols Then
    5.         lngMoveColumn = grdDetails.MouseCol
    6.     Else
    7.         lngMoveColumn = -1
    8.     End If
    9. End Sub
    10.  
    11. Private Sub grdDetails_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    12.     If grdDetails.MouseRow = 0 And lngMoveColumn <> -1 Then
    13.         grdDetails.ColPosition(lngMoveColumn) = grdDetails.MouseCol
    14.     End If
    15. End Sub

  4. #4

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: FlexGrid Sorting/Rearranging Columns

    Is there a way to allow multiple selections on a FlexGrid using CTRL? Right now all I can do is hold SHIFT and select ranges. Is there away to allow multiple (individual) selections using CTRL like in Windows Explorer and other environments?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: FlexGrid Sorting/Rearranging Columns

    I'm starting to think doing a multiple select using CTRL isn't possible after looking at all the properties of the FlexGrid.

    Does anyone know of a way to do this?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: FlexGrid Sorting/Rearranging Columns

    The FlexGrid does not support the "extended selection" functionality. But it is easy enough to simulate. This code is for the Hierarchical flex grid but it should work on just the FlexGrid.

    Basically, it uses the back ground colors to simulate selecting multiple non-contiguous rows.

    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     MSHFlexGrid1.HighLight = flexHighlightNever
    4.  
    5. End Sub
    6.  
    7. Private Sub MSHFlexGrid1_Click()
    8.     Dim lngColor As Long
    9.    
    10.     With MSHFlexGrid1
    11.         .Redraw = False
    12.  
    13.         lngColor = .BackColor
    14.    
    15.         If .CellBackColor = lngColor Then
    16.             lngColor = .BackColorSel
    17.         End If
    18.    
    19.         .FillStyle = flexFillRepeat
    20.         .Col = 1
    21.         .ColSel = .Cols - 1
    22.         .CellBackColor = lngColor
    23.         .FillStyle = flexFillSingle
    24.        
    25.         .Redraw = True
    26.    
    27.     End With
    28.    
    29. End Sub

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: FlexGrid Sorting/Rearranging Columns

    Only in BigSelection's. There is two options, one for using Control and the other for select each.

  8. #8

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: FlexGrid Sorting/Rearranging Columns

    Quote Originally Posted by brucevde
    The FlexGrid does not support the "extended selection" functionality. But it is easy enough to simulate. This code is for the Hierarchical flex grid but it should work on just the FlexGrid.

    Basically, it uses the back ground colors to simulate selecting multiple non-contiguous rows.

    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     MSHFlexGrid1.HighLight = flexHighlightNever
    4.  
    5. End Sub
    6.  
    7. Private Sub MSHFlexGrid1_Click()
    8.     Dim lngColor As Long
    9.    
    10.     With MSHFlexGrid1
    11.         .Redraw = False
    12.  
    13.         lngColor = .BackColor
    14.    
    15.         If .CellBackColor = lngColor Then
    16.             lngColor = .BackColorSel
    17.         End If
    18.    
    19.         .FillStyle = flexFillRepeat
    20.         .Col = 1
    21.         .ColSel = .Cols - 1
    22.         .CellBackColor = lngColor
    23.         .FillStyle = flexFillSingle
    24.        
    25.         .Redraw = True
    26.    
    27.     End With
    28.    
    29. End Sub
    If I understand this right then I have 2 problems with it.

    1) I don't have a way to check which rows are selected (I have selection mode set to row) without checking the background color of every row in the table.
    2) I want it to only select multiple rows only if CTRL is pressed and I want it to select ranges if SHIFT is pressed. It is easy enough to add the CTRL part useing a module level variable. But I'm not 100% sure how to select ranges using this method.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  9. #9
    New Member
    Join Date
    Nov 2007
    Posts
    8

    Re: FlexGrid Sorting/Rearranging Columns

    tryign to get on form load for it to move column 4 to column 1 automaticlly

    constant errors on everythign I tried so far

    wondering if you guys could help me out??

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: FlexGrid Sorting/Rearranging Columns

    Welcome to VBForums

    Based on post #3 above, this should do it:
    Code:
    MSFlexGrid1.ColPosition(3) = 1

  11. #11
    New Member
    Join Date
    Nov 2007
    Posts
    8

    Re: FlexGrid Sorting/Rearranging Columns

    dang your good and quick!!

    worked like a charm, I was wayyyy over complicating things, now if to figgure out how to sort dates when they are like yyyy/mm/dd

    hate to be constantly asking for help but skill arent what they used to be for vb, too many years of linux programming, back on the redmond side for a change :-S

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: FlexGrid Sorting/Rearranging Columns

    When the format is yyyy/mm/dd a simple string sort will work.

    Code:
    Private Sub Form_Load()
        With Me.MSFlexGrid1
            .Rows = 5
            .TextMatrix(1, 1) = "2007/06/23"
            .TextMatrix(2, 1) = "2007/01/05"
            .TextMatrix(3, 1) = "2007/06/03"
            .TextMatrix(4, 1) = "2007/01/01"
            .Sort = flexSortStringAscending
        End With
    End Sub

  13. #13
    New Member
    Join Date
    Nov 2007
    Posts
    8

    Re: FlexGrid Sorting/Rearranging Columns

    man yee guys are good sooooooOOOooOo many thanks

    thank you all for your time in helping me, if I could only be as helpful to others as you are to me

    many many thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width