17.7.4 GridView - Sorting the data

The GridView component does not have its own method for sorting the data in the grid column by column, although the Sorted property exists for the GridView. You have to program the missing sorting method yourself, as a look in the help shows: Sorting the data is not done automatically. It must be done by user code. In chapter 7.4 Array you will find the basic procedures for sorting a derived array. The basic idea is to store all rows of the GridView in an array, sort the array by a given column and then fill the GridView with the elements of the sorted array using the'ArrayToGrid' procedure.

The main program uses the two classes Variant[].class and CSort.class from chapter 7 to display the columns in the grid view in ascending or descending order. The corresponding places are highlighted in color in the (complete) source text:

[1] ' Gambas class file
[2] 
[3] Private hSort As New CSort
[4] Private iLast As Integer = -1
[5] Private iCount As Integer
[6] 
[7] Public Sub Form_Open()
[8] 
[9]   GridView1.Header = GridView1.Horizontal
[10]   GridView1.Sorted = True
[11]   
[12]   GridView1.Columns.Count = 6
[13]   
[14]   GridView1.Columns[0].Width = 90
[15]   GridView1.Columns[0].Title = "Number"
[16]   GridView1.Columns[1].Width = 90
[17]   GridView1.Columns[1].Title = "Boolean"
[18]   GridView1.Columns[2].Width = 80
[19]   GridView1.Columns[2].Title = "String"
[20]   GridView1.Columns[3].Width = 160
[21]   GridView1.Columns[3].Title = "Date1"
[22]   GridView1.Columns[4].Width = 90
[23]   GridView1.Columns[4].Alignment = Align.Center
[24]   GridView1.Columns[4].Title = "Number2"
[25]   GridView1.Columns[5].Title = "Date2"
[26]     
[27]   FMain.Center
[28]   FMain.Resizable = False
[29]   iCount = 999
[30]   FMain.Text = "Ausgabe von " & Str((iCount + 1) * Str(GridView1.Columns.Count)) & " Zufallsdaten, \\ die sortiert werden können!"
[31] End ' Form_Open
[32] 
[33] Public Sub btnFillGrid_Click()
[34]   Dim i As Integer
[35]   Dim vMatrix As Variant[]
[36]   Dim aStrs As String[] = ["Merkur", ..., "Mars", "Jupiter", "Saturn", "Uranus", "Neptun", "Pluto"]
[37] 
[38]   hSort.Clear() ' Spaltensortierung zurücksetzen
[39]   iLast = -1
[40]   
[41]   GridView1.Clear
[42]   Randomize
[43]   
[44]   For i = 0 To iCount Step 1
[45]       vMatrix = New Variant[]
[46]       vMatrix.Add(Round(Rnd(0, 10), -2)) ' Reelle Zahl
[47]       vMatrix.Add(CBool(Round(Rnd(0, 1)))) ' Wahrheitswert
[48]       vMatrix.Add(aStrs[CInt(Rnd(0, aStrs.Count))]) ' Zeichenkette
[49]       vMatrix.Add(CDate(Rnd(CFloat(Now()), CFloat(Now() + 1000)))) ' Datum 1
[50]       vMatrix.Add(CInt(Rnd(-10, 10))) ' Ganze Zahl   
[51]       vMatrix.Add(CDate(Rnd(CFloat(Now()), CFloat(Now() + 2000)))) ' Datum 2
[52]       hSort.Add(vMatrix)
[53]   Next ' i
[54]   
[55]   ArrayToGrid()
[56] 
[57] End ' FillGrid
[58] 
[59] Public Sub ArrayToGrid()
[60]   Dim i As Integer
[61]   Dim vMatrix As Variant[]
[62]   
[63]   GridView1.Rows.Count = hSort.Count
[64]   For i = 0 To hSort.Count - 1 Step 1
[65]       vMatrix = hSort[i]
[66]       GridView1[i, 0].Text = Str$(vMatrix[0])
[67]       GridView1[i, 1].Text = Str$(vMatrix[1])
[68]       GridView1[i, 2].Text = Str$(vMatrix[2])
[69]       GridView1[i, 3].Text = Str$(vMatrix[3])
[70]       GridView1[i, 4].Text = Str$(vMatrix[4])
[71]       GridView1[i, 5].Text = Str$(vMatrix[5])
[72]   Next ' i
[73] 
[74] End '  ArrayToGrid
[75] 
[76] Public Sub GridView1_ColumnClick(Column As Integer)
[77] ' Beim wiederholten Klick auf die gleiche Spalte wird die Sortierung invertiert  
[78]   If iLast = Column Then
[79]      hSort.Reverse() ' Optimierung, um nicht mit gb.Descent wiederholt zu sortieren
[80]   Else
[81]      hSort.SortField(Column)
[82]   Endif ' iLast
[83]   
[84]   ArrayToGrid()
[85]   iLast = Column
[86] End ' GridView1_ColumnClick(..)

Sortierung GridView1

Figure 17.7.4.1: Sorting by the 1st column in the GridView - ascending

Sortierung GridView1

Figure 17.7.4.2: Sorting by the 1st column in the GridView - descending

17.7.4 Download