sorting, searching, and filtering rows/columns in a flexgrid


Hello everyone!! I'm working on a project, and ran across a problem that I think would make an excellent tutorial topic. I am using a flexgrid in a program that I'm writing to display user log-on information. The data is stored in a comma delimited text file "uspass.txt", and the resulting table may have any number of columns, and any number of rows depending on the data in that file. Ultimately this data will be encrypted, but during the design phase I have left it unsecured. The table that is generated has rows of alternating color, and has one fixed row as a header, which I accomplished with this code:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub Form_Load()
Dim empnum As Integer
Dim use As String
Dim pass As String
With MSFlexGrid1
.Cols = 2
.Rows = 1
End With
MSFlexGrid1.Col = 0
MSFlexGrid1.Text = "USER"
MSFlexGrid1.Col = 1
MSFlexGrid1.Text = "PASSWORD"
empnum = FreeFile
Open (Left(CurDir, 3) & "Office351\uspass.dat") For Input As #empnum

Do Until EOF(empnum)
MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
MSFlexGrid1.Row = MSFlexGrid1.Rows - 2
MSFlexGrid1.Col = 0
If MSFlexGrid1.CellBackColor = &HFFC0C0 Then
MSFlexGrid1.Row = MSFlexGrid1.Rows - 1
MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
MSFlexGrid1.FillStyle = flexFillRepeat
MSFlexGrid1.CellBackColor = &HFFFFC0
Else: MSFlexGrid1.Row = MSFlexGrid1.Rows - 1
MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
MSFlexGrid1.FillStyle = flexFillRepeat
MSFlexGrid1.CellBackColor = &HFFC0C0
End If

MSFlexGrid1.Col = 0
Input #empnum, use, pass

MSFlexGrid1.Text = use
MSFlexGrid1.Col = 1
MSFlexGrid1.Text = pass
Loop
Close #empnum
Form4.Text2.Text = "Total number of users: " & MSFlexGrid1.Rows - 1
MSFlexGrid1.Row = 1
MSFlexGrid1.Col = 0

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
The data which is loaded, is in no real alphabetic or numeric order, but if I try to use the flexgrid's built in "sort" function, the result is that all of the cell's back color is moved with the data. Disrupting my altering colors, and creating numerous "blocks" of colors, is visually damaging, and nullifies the benefits of those alternating colors (for e.g. being able to quickly scan over all of the columns and not get confused as to the row your on. So I wrote my own sorting function at this point with the code that follows.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim xindex As Long
Dim yindex As Long
Dim xtxt As String
Dim ytxt As String
Dim scol As Long
Dim xcol As Long

scol = MSFlexGrid1.Col

For xindex = 1 To Form4.MSFlexGrid1.Rows - 2
For yindex = xindex + 1 To Form4.MSFlexGrid1.Rows - 1
MSFlexGrid1.Col = scol
MSFlexGrid1.Row = xindex
xtxt = MSFlexGrid1.Text
MSFlexGrid1.Row = yindex
ytxt = MSFlexGrid1.Text
If UCase(ytxt) < UCase(xtxt) Then
For xcol = 0 To Form4.MSFlexGrid1.Cols - 1
MSFlexGrid1.Col = xcol
MSFlexGrid1.Row = xindex
xtxt = MSFlexGrid1.Text
MSFlexGrid1.Row = yindex
ytxt = MSFlexGrid1.Text
MSFlexGrid1.Text = xtxt
MSFlexGrid1.Row = xindex
MSFlexGrid1.Text = ytxt
Next xcol
End If
Next yindex
Next xindex
End Sub
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Now as the form loads that data is sorted!! The process is fast and effective, AND leaves the alternating colors. YEAH!!! Here comes the problem :( I want to be able to click the column header and have a seperate form appear that will allow me to sort ascending, sort decending, search, and filter whichever column I have clicked. So I designed a simple form with those buttons on it. And I use this code to activated it (and determine the column).
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub MSFlexGrid1_Click()
If MSFlexGrid1.MouseRow = 0 Then
MSFlexGrid1.Row = 0
FormSort.Caption = MSFlexGrid1.Text
FormSort.Top = Form4.Top + MSFlexGrid1.Top + MSFlexGrid1.CellTop + MSFlexGrid1.CellHeight + 375
FormSort.Left = Form4.Left + MSFlexGrid1.Left + MSFlexGrid1.CellLeft - FormSort.Width
FormSort.Show

End If
End Sub
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
This too works! Now for the Sorting buttons I already have the code. For the sort ascending button I just put the second sample of code from above into the click event of the button. For the sort decending I use the same code with one minor change... on the line containing the "If" statement I change the "<" operand to a ">" operand. VOILLA!!! This should all work great!! And, yes it does work... but the :((( THERE IS A MASSIVE DELAY WHILE I HAVE TO SIT THERE AND WATCH THE FLEXGRID DO IT'S SORTING!! I can only imagine what it will be like when i get to the searching and filtering buttons where I would like to have a textbox on a seperate form appear and the grid respond to each keystroke entered into that box. Please Help!!!

If you enjoyed this post, subscribe for updates (it's free)

Woke up 2day and realized the solution..

Yes I woke up and started thinking on the problem and I realized that I knew the solution. In fact, the solution came to me as I thought about this post. I stated that I had to sit here and watch the FlexGrid do its sorting. Well... by disabling its redraw property, I solved both of the issues. I just added these two lines of code to the button click events:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub SortDowncmd_Click()
Dim xindex As Long
Dim yindex As Long
Dim xtxt As String
Dim ytxt As String
Dim scol As Long
Dim xcol As Long
Form4.MSFlexGrid1.Redraw = False '<<<<<<<<<<<
...
Form4.MSFlexGrid1.Redraw = True '<<<<<<
Me.Hide
End Sub
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
And problem solved!!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.